0

I have a function below which has an input Date and it will return the first and last Date of the next month in MM/dd/yyyy format.

String string = "01/01/2022";
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date dt = sdf .parse(string);
Calendar c = Calendar.getInstance();
c.setTime(dt);
c.add(Calendar.MONTH, 1);  
String firstDate = sdf.format(c.getTime());
System.out.println("FirstDate:" + firstDate);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DAY_OF_MONTH, -1);
String lastDate = sdf.format(c.getTime());
System.out.println("LastDate:" + lastDate);

The above will give me an output like below

FirstDate:02/01/2022
LastDate:02/28/2022

This works well if the input is the first day of the previous month, what i would like to achieve is to get the FirstDate and LastDate of the next month even if the input is a date which is not the first date of the month for example 01/31/2022 gives me the output below

FirstDate:02/28/2022
LastDate:03/27/2022

But i would still like it to give me the first out of

FirstDate:02/01/2022
LastDate:02/28/2022
1
  • 2
    I strongly recommend you don’t use DateFormat, SimpleDateFormat, Date and Calendar. Those classes are notoriously troublesome and long outdated. Instead use LocalDate and DateTimeFormatter, both from java.time, the modern Java date and time API.
    – Anonymous
    Commented May 20, 2022 at 13:57

2 Answers 2

5

Don't use Date as it is obsolete and buggy. Use LocalDate and other classes from the java.time package.

  • the following takes an existing date first, adds 1 to the month. This will also cause the year to increase if required.
  • then the dayOfMonth as either 1 or the last day of the month. Leap years are automatically considered.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy");
LocalDate date = LocalDate.parse("12/22/2020", dtf);
date = date.plusMonths(1);
LocalDate endDate = date.withDayOfMonth(date.lengthOfMonth());
LocalDate startDate = date.withDayOfMonth(1);
System.out.println("FirstDate: " +startDate.format(dtf));
System.out.println("LastDate:  " +endDate.format(dtf));

prints

FirstDate: 01/01/2021
LastDate:  01/31/2021


0
5

You can have that easier in Java 8. Use a java.time.YearMonth, get the current one with its method now() and derive the first and last LocalDate of it:

public static void main(String[] args) {
    // get the current month
    YearMonth currentMonth = YearMonth.now();
    // get the date with day of month = 1 using the current month
    LocalDate firstOfMonth = currentMonth.atDay(1);
    // then get its last date (no number required here)
    LocalDate lastOfMonth = currentMonth.atEndOfMonth();
    // prepare a formatter for your desired output (default: uuuu-MM-dd)
    DateTimeFormatter customDtf = DateTimeFormatter.ofPattern("MM/dd/uuuu");
    // print the month and year without a formatter (just for visualization)
    System.out.println("Month:     " + currentMonth);
    // then print both desired dates using the custom formatter
    System.out.println("FirstDate: " + firstOfMonth.format(customDtf));
    System.out.println("LastDate:  " + lastOfMonth.format(customDtf));
}

This prints

Month:     2022-05
FirstDate: 05/01/2022
LastDate:  05/31/2022

You can – of course – use any given month, there is YearMonth.of(int year, int month) which you could use in order to create your example value:

YearMonth currentMonth = YearMonth.of(2022, 2);
4
  • 1
    Where is the input for date date Commented May 20, 2022 at 13:45
  • 1
    Better to provide the first and last day of next month as question required.
    – samabcde
    Commented May 20, 2022 at 13:46
  • You‘re right @samabcde, I will change that accordingly soon…
    – deHaar
    Commented May 20, 2022 at 14:30
  • @EmmanuelNjorodongo sorry, just forgot the input… would basically lead to the accepted answer, so I will leave this for future readers who only read your question title…
    – deHaar
    Commented May 20, 2022 at 14:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.