Handling of time zones using Java

Before it grows on the subject

When dealing with a time zone using java.util.Date in Java, I think that it is common that the date and time are the elapsed time (milliseconds) from the epoch time and the time zone is managed by GMT. , Here we assume that the date and time and the timezone are kept separate for some reason. For example, it looks like this

item value
Date and time 2016-12-01 15:00:00
Time zone Australia/Sydney

When this is displayed by System.out.println etc., the value held by Date type feels strange as shown below. In other words, I want 15:00 to be Australia / Sydney time, but for some reason it's a local time zone. It's like that.

Thu Dec 01 15:00:00 JST 2016

sun.util.calendar.ZoneInfo[id="Asia/Tokyo",offset=32400000,dstSavings=0,useDaylight=false,transitions=10,lastRule=null]

Also, ** If you use the conversion done here, you will run a bug, so on the other hand, please do not reach this logic as a teacher. ** If you find a solution, I will post it again. I'm not sure if the situation is avoided.

Main subject

Anyway, I'm thinking about how to keep it as correct data when the above situation occurs. Avoiding such situations may be the best solution.

For Java 8 or earlier

It's a little tricky code. It's a painstaking measure to convert it to a character string.

TimezoneConversion.java


    static Date convertWithBeforeJava7(Date dateTime, TimeZone timeZone) throws ParseException {
        String format = "yyyy/MM/dd HH:mm:ss.SSS";
        SimpleDateFormat local = new SimpleDateFormat(format);
        SimpleDateFormat convert = new SimpleDateFormat(format);
        convert.setTimeZone(timeZone);
        return convert.parse(local.format(dateTime));
    }

Java8

I can't get used to it, but I also tried it in Java 8 classes. It's refreshing somehow, but I also feel like I'm going to go back to the Date type.

test results

I did two types of tests. The input is as follows.

Environmental condition

item value
Local time zone Australia/Sydney

input

Test number Date and time Time zone
1 2016-10-02 02:00 Australia/Sydney
2 2016-10-02 02:00 America/Montreal

Please note that it is not a sufficient test. The result is as follows.

java.lang.AssertionError: It would be same date
Expected: is <Sun Oct 02 17:00:00 EST 2016>
     but: was <Sun Oct 02 18:00:00 EST 2016>

	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:956)
	at com.nekoscape.java.sample.TimezoneConversionTest.convertWithOtherTimezone_test_for_Java8Method(TimezoneConversionTest.java:76)
(abridgement)

java.lang.AssertionError: It would be same date
Expected: is <Sun Oct 02 17:00:00 EST 2016>
     but: was <Sun Oct 02 18:00:00 EST 2016>

	at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
	at org.junit.Assert.assertThat(Assert.java:956)
	at com.nekoscape.java.sample.TimezoneConversionTest.convertWithOtherTimezone_test_for_beforeJava7Method(TimezoneConversionTest.java:62)
(abridgement)

Process finished with exit code -1

Two tests have failed. Both occur when you specify America / Montreal, which is not a local timezone. What's happening?

The bottom line is that this logic now has problems when the local timezone is in daylight saving time. The date and time specified as 2016-10-02 02:00 is the start date and time of daylight saving time in Australia / Sydney. Therefore, when it is retained as Date type from the character string, it already means 2016-10-02 03:00. As a result, there is an hour of conversion error.

As mentioned at the beginning, it is a bug. I haven't solved it. Please do not copy even if you make a mistake. The source is placed below so that you can try it out. Source code: timezone-conversion

Recommended Posts

Handling of time zones using Java
[Java] Practice of exception handling [Exception]
[Note] Handling of Java decimal point
Step-by-step understanding of Java exception handling
Summary of object-oriented programming using Java
I tried using GoogleHttpClient of Java
Status monitoring of java application using Elasticsearch
Acquisition of input contents using Scanner (Java)
Java exception handling?
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
[Java] Overview of Java
[Java] Handling of JavaBeans in the method chain
Verification of performance impact when using Java volatile
Feel the passage of time even in Java
Try similar search of Image Search using Java SDK [Search]
Example of using addition faster than using StringBuilder (Java)
The date time of java8 has been updated
Story of test automation using Appium [Android / java]
Expired collection of java
Sorting using java comparator
Predicted Features of Java
[Java] Significance of serialVersionUID
[Java] Date / time operations
NIO.2 review of java
Review of java Shilber
Accurate time measurement (Java)
java --Unification of comments
Scraping practice using Java ②
History of Java annotation
Summary of using FragmentArgs
java (merits of polymorphism)
Scraping practice using Java ①
NIO review of java
[Java10] Be careful of using var and generics together
[Java] Three features of Java
Summary of using DBFlow
Summary of Java support 2018
[Java] Handling of character strings (String class and StringBuilder class)
Handling of date and time in Ruby. Use Date and Time properly.
[Java] Comparison method of character strings and comparison method using regular expressions
Using multiple versions of Java with Brew on Mac + jEnv
Check the status of Java application without using monitoring tool
[Java] Try editing the elements of the Json string using the library
The story of pushing Java to Heroku using the BitBucket pipeline
[Introduction to Java] Handling of character strings (String class, StringBuilder class)
Example of params using where
About the handling of Null
About an instance of java
Handling of SNMP traps (CentOS 8)
[Java] Mirage-Basic usage of SQL
[Java] Beginner's understanding of Servlet-②
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
Try scraping using java [Notes]
Summary of using Butter Knife
[Java] Creation of original annotation
4th day of java learning
[Java] Beginner's understanding of Servlet-①