Wrong last day of the month

Where is some function to get the last day of the month in my service?

DateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); Date date = format.parse(stringDate); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DATE, -1); Date lastDayOfMonth = calendar.getTime(); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(lastDayOfMonth); 

So, this method works correctly elsewhere, but in the USA the last day is always 29 (the last day is 1)

stringDate is a date in the format "yyyy-MM-dd"

+6
source share
5 answers

I believe this problem is related to US Daylight Savings .

You can change this by setting the time zone for the calendar to a different time zone.

Related question: Adding days with java.util.Calendar gives strange results

+4
source

Java Date has a very bad API. Instead, I would recommend that you use Joda Time .

In Joda, it will look like this:

 LocalDate endOfMonth = date.dayOfMonth().withMaximumValue(); 
+1
source

If you do not have Java 8, it is very compact with JodaTime.

 import org.joda.time.DateTime; public class SoLastDay { public DateTime lastDay(final String yyyy_MM_dd) { DateTime givenDate = new DateTime(yyyy_MM_dd); return givenDate.dayOfMonth().withMaximumValue(); } } 

And a little test ...

 @Test public void testLastDay() throws Exception { SoLastDay soLastDay = new SoLastDay(); String date1 = "2015-01-27"; System.out.printf("Date %s becomes %s.\n", date1, soLastDay.lastDay(date1).toString("yyyy-MM-dd")); String date2 = "2015-02-02"; System.out.printf("Date %s becomes %s.\n", date2, soLastDay.lastDay(date2).toString("yyyy-MM-dd")); } 

And the test results:

 Date 2015-01-27 becomes 2015-01-31. Date 2015-02-02 becomes 2015-02-28. 
0
source

If you have Java 8, you can use this code:

 import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; public class SoLastDayJava8 { static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public LocalDate lastDay(final String yyyy_MM_dd) { LocalDate givenDate = LocalDate.parse(yyyy_MM_dd, formatter); return givenDate.with(TemporalAdjusters.lastDayOfMonth()); } } 

The test code changes a bit.

 public class SoLastDayJava8Test { @Test public void testLastDay() throws Exception { SoLastDayJava8 soLastDay = new SoLastDayJava8(); String date1 = "2015-01-27"; System.out.printf("Date %s becomes %s.\n", date1, soLastDay.lastDay(date1)); String date2 = "2015-02-02"; System.out.printf("Date %s becomes %s.\n", date2, soLastDay.lastDay(date2)); } } 

But the results are the same.

The date 2015-01-27 becomes 2015-01-31.

The date 2015-02-02 becomes 2015-02-28.

0
source

You messed with TimeZones .

When you do Date date = format.parse(stringDate); , you create a Date object with a TimeZone a DateFormat object. Theoretically, if TimeZone same for all DateFormat and Calendar objects, you should be fine. Check if they getTimeZone() method.

If the TimeZone first DateFormat is incorrect (for example, your TimeZone or UTC or GMT ), you will get a UTC-008 conversion in the second TimeZone (and in Calendar ), which will lead to an absent day since you start at midnight.

Judging by your code, the stringDate itself, which was incorrectly converted somewhere else ...

0
source

Source: https://habr.com/ru/post/981610/


All Articles