0

We will pass month information in yyyyMM format.From that given input how i will get FirstDate and LastDate of that month?

Input:-202002

OutPut Should be:-

First Date:- 2020-02-01,
Last Date:- 2020-02-29

DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
Date date = new Date();
System.out.println("First Date:- "+dateFormat.format(date));    
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
Date previousmonth = calendar.getTime();
System.out.println("LastDate:-"+dateFormat.format(previousmonth).toString());
3
  • Do you mean day instead of date? Because the first date of every month is 1 and the last of most is 31, 30 or 28. Commented Feb 20, 2020 at 10:22
  • 3
    Prefer to use java.util.time classes. The YearMonth class provides an easy atEndOfMonth() method. As for the first date of the month, it's the 1st. Just use atDay(1) Commented Feb 20, 2020 at 10:22
  • 1
    @kumesana there's no such thing as java.util.time. I think you mean java.time. Commented Feb 20, 2020 at 10:35

3 Answers 3

2

As I just learned, there are now special methods for it.

You can do it like this now:

public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");
public static final DateTimeFormatter OUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

public static LocalDate[] getFirstAndLastDateOfMonthNew(String yearAndMonth) {
    final LocalDate[] result = new LocalDate[2];
    final YearMonth yearMonth = YearMonth.parse(yearAndMonth, FORMATTER);
    result[0] = yearMonth.atDay(1);
    result[1] = yearMonth.atEndOfMonth();;
    return result;
}

The old way would have been:

Just add the 01 to the date and parse it. The add a month and subtract a day.

public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
public static final DateTimeFormatter OUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

public static LocalDate[] getFirstAndLastDateOfMonth(String yearAndMonth) {
    final LocalDate[] result = new LocalDate[2];
    final LocalDate first = LocalDate.parse(yearAndMonth + "01", FORMATTER);
    final LocalDate last = first.plusMonths(1).minusDays(1);
    result[0] = first;
    result[1] = last;
    return result;
}
Sign up to request clarification or add additional context in comments.

2 Comments

... Or, like, use YearMonth
Yep, just learned that. Thank you. I updated my answer.
1

try this, you can do it simply with LocalDate

String date = "202002";

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                .appendPattern("yyyyMM")
                                .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                                .toFormatter();

LocalDate firstDay = LocalDate.parse(date, formatter);
LocalDate lastDay = firstDay.withDayOfMonth(firstDay.getMonth().length(firstDay.isLeapYear()));

Comments

0

You can use java 8 capabilities like TemporalAdjusters I think it May help you in your problem like the following

    public static void getFirstDayAndLastDayInMonth(String date){
        LocalDate monthOfYear=LocalDate.parse(new String(date+"01"),DateTimeFormatter.BASIC_ISO_DATE);
        LocalDate firstLocalDate=monthOfYear.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(firstLocalDate);
        LocalDate lastLocalDate=monthOfYear.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(lastLocalDate);
    }

input 202002

output

2020-02-01
2020-02-29

and you can follow the following doc it's a good start to learn more

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.