The relationship between strict Java date checking and daylight savings time

Strict date check

DateFormat # setLenient (boolean) is a setting for ** loose date checking **. It is used in SimpleDateFormat etc. The default is true, which does not strictly check. If you want a strict check, set false. [^ 1] [^ 1]: It's the opposite of not being strict (don't design the boolean value to be reversed depending on how you think about it). Not loose. Not loose. Gachi. By the way, from Java 8, you can use Enumeration ResolverStyle with DateTimeFormatter. I am.

Reference: Open JDK DateFormat # setLenient (boolean), Oracle Java DateFormat # setLenient (boolean))

If you don't do a strict check, it will be interpreted as 3/1 even if it is 2/29 when it is not a leap year.

Daylight saving time

It's not very familiar in Japan, but it's said that it will be one hour earlier only in the summer that [^ 2]. In Eastern Time (EST / EDT) in New York, [^ 3] is set as daylight saving time from 2:00 am on the second Sunday in March to 2:00 am on the first Sunday in November, and this period is advanced by one hour. [^ 2]: Daylight saving time, daylight saving time, daylight saving time, sunmer time. It seems that it used to be in Japan. Depending on the area, it may be 30 minutes instead of 1 hour. [^ 3]: Until 2006, it was from 2:00 am on the first Sunday in April to 2:00 am on the last Sunday in October. It depends on the law.

2020/03/08 02:00:00 becomes an error

private static void datechange(String date) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    sdf.setLenient(false); //Not loose
    sdf.setTimeZone(TimeZone.getTimeZone("US/Eastern")); // EDT
    try {
        Date pdate = sdf.parse(date);
        System.out.println(sdf.format(pdate));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
//Start of daylight saving time(1 hour ahead)
datechange("2020/03/08 01:59:59"); // OK
datechange("2020/03/08 02:00:00"); // NG: ParseException
datechange("2020/03/08 02:00:01"); // NG: ParseException
datechange("2020/03/08 02:59:59"); // NG: ParseException
datechange("2020/03/08 03:00:00"); // OK

//End of daylight saving time(1 hour back)
datechange("2020/11/01 01:59:59"); // OK
datechange("2020/11/01 02:00:00"); // OK
datechange("2020/11/01 02:00:01"); // OK
datechange("2020/11/01 02:59:59"); // OK
datechange("2020/11/01 03:00:00"); // OK
java.text.ParseException: Unparseable date: "2020/03/08 02:00:00"
	at java.text.DateFormat.parse(DateFormat.java:357)

If you are doing a strict check setLenient (false) and the timezone is ʻEDT, parsing 2020/03/08 02:00:00will result injava.text.ParseException` ..

Even if it is not ʻEDT`, an error will occur if ** "1 hour skipped when daylight saving time comes" ** is specified in the time zone that considers daylight saving time.

In other words, 2020/03/08 02:00:00 does not exist in ** ʻEDT` **.

There is no error when returning for 1 hour. It's a little different from this story, but if you set the date as a unique key when going back one hour, it will be a unique constraint violation. Sorting doesn't work as expected either. [^ 4] [^ 4]: Let's hope that there is no system that registers the local time in the DB as a character string. (Usually it is a date type that is not affected by the time zone, UTC even if it is a character string, or epoch seconds as a numerical value)

Root cause

Since we have specified a time that does not exist, the problem is not with the above specifications but with the original data and the capture process. In this example, the cause was that the original data was JST instead of ʻEDT`. (Time zone inconsistency between original data [JST] and import process [EDT])

Recommended Posts

The relationship between strict Java date checking and daylight savings time
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
[Java] Relationship between H2DB and JDBC
[MySQL] [java] Receive date and time
About the relationship between the Java String equality operator (==) and initialization. Beginners
Relationship between kotlin and java access modifiers
[Java] How to set the Date time to 00:00:00
[Java] How to get the current date and time and specify the display format
Interpret the relationship between Java methods and arguments into a biochemical formula
Date and time
[Java] Understand the difference between List and Set
Format the date and time given by created_at
Summarize the differences between C # and Java writing
The date time of java8 has been updated
[Java] Get and display the date 10 days later using the Time API added from Java 8.
Parse the date and time string formatted by the C asctime function in Java
About the relationship between HTTP methods, actions and CRUD
[JAVA] What is the difference between interface and abstract? ?? ??
Learn for the first time java # 3 expressions and operators
Verification of the relationship between Docker images and containers
What is the difference between Java EE and Jakarta EE?
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Difference between Java and JavaScript (how to find the average)
Set the date and time from the character string with POI
Sample code to parse date and time with Java SimpleDateFormat
[Java] Check the difference between orElse and orElseGet with IntStream
Think about the differences between functions and methods (in Java)
Relationship between Controller and View
Relationship between package and class
Check the actual date and time at parse with Java's SimpleDateFormat
Get the current date and time by specifying the time zone in Thymeleaf
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
The design concept of Java's Date and Time API is interesting
[Java] What is the difference between form, entity and dto? [Bean]
Differences between "beginner" Java and Kotlin
[Java] Difference between Hashmap and HashTable
Relationship between ActiveRecord with_lock and cache
Java release date and EOL summary
Understand the difference between each_with_index and each.with_index
Relationship between database and model (basic)
[JAVA] Difference between abstract and interface
[Java] Difference between array and ArrayList
Differences between Java and .NET Framework
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
A note on the differences between interfaces and abstract classes in Java
This and that of the implementation of date judgment within the period in Java
Organize the relationship between SLF4J, Logback, and Log4J along with their behavior
I translated the grammar of R and Java [Updated from time to time]
Understand the difference between int and Integer and BigInteger in java and float and double
Java unit test Same day judgment ArDate, Date, Calendar, long ignore the time and judge whether they are the same day
Java Unit Test Library-Determine if Artery-Date, Calendar, long is the current date and time (specify tolerance in minutes)