Search code examples
javajodatimedate-conversion

JodaTime conversion by the end of the month


I got pretty straight forward DateConverter with method that converts String to org.joda.time.DateTime

public class DateConverter {
    private static final DateTimeFormatter DATE_TIME_FORMATTER =
            DateTimeFormat
                    .forPattern( "yyyy-MM-dd HH:mm:ss Z")
                    .withZone( DateTimeZone.forID("Europe/Warsaw") );

    public static DateTime toDateTime(String value) {
        return DateTime.parse(value, DATE_TIME_FORMATTER);
    }
}

Now to test it,

 String okDate = "2018-10-28 00:00:00 +0200";
 String wrongDate = "2018-10-29 00:00:00 +0200";        

 System.out.println("Ok result: " + DateConverter.toDateTime(okDate));
 System.out.println("Wrong result: " + DateConverter.toDateTime(wrongDate));

Which leaves me with output:

Ok result: 2018-10-28T00:00:00.000+02:00
Wrong result: 2018-10-28T23:00:00.000+01:00

All days till end of the month, 29th, 30th, 31th are like 28th. Rest of days are ok. Can someone help me understand what is going on here? Where is the misstake I made?


Solution

  • Uh. You specified a timezone with a DST change at that date, so works as intended.

    If you want to enter local datetime, you don't want the Z in your format.