Home > Net >  Java different timezone offset in same timezone
Java different timezone offset in same timezone

Time:12-30

I'm using "Asia/Bangkok" zone id. That offset is from GMT UTC 07:00.

but when I did followings, then it is not 7:00 when set "01/01/1900 7:00:00.000"

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
Date date = dateFormat.parse("01/01/1900 7:00:00.000");

System.out.println(date);
System.out.println(date.getTimezoneOffset());

Date date2 = dateFormat.parse("01/01/1900 6:00:00.000");
System.out.println(date2);
System.out.println(date2.getTimezoneOffset());

The result is

Mon Jan 01 07:00:00 ICT 1900
-402
Mon Jan 01 06:00:00 ICT 1900
-420

I wondered if the offset had changed around 7:00 a.m. on January 1, 1900, so I looked it up on Wikipedia. https://en.wikipedia.org/wiki/Time_in_Thailand

It was UTC 6:42, but from 1880 to 1920.

I have 3 questions.

  1. Why it happen different time offset between "01/01/1900 7:00:00.000" and "01/01/1900 6:00:00.000"
  2. Where can I see time zone history in Java.
  3. How can I ignore different time offset in same Timezone.

CodePudding user response:

Java runtime timezone information for each version is available here https://www.oracle.com/java/technologies/tzdata-versions.html

Inside the linked file (for a specific version) you can find links to the actual data used

https://www.iana.org/time-zones/repository/releases/tzcode2021a.tar.gz
https://www.iana.org/time-zones/repository/releases/tzdata2021a.tar.gz
https://www.iana.org/time-zones/repository/releases/tzdb-2021a.tar.lz

Inside the tzdata*.tar.gz you can find a file called asia which contains the data for Bangkok as well.

It contains these entries

# Thailand
# Zone  NAME            STDOFF  RULES FORMAT [UNTIL]
Zone    Asia/Bangkok    6:42:04 -     LMT    1880
                        6:42:04 -     BMT    1920 Apr # Bangkok Mean Time 
                        7:00    -      07
Link Asia/Bangkok Asia/Phnom_Penh   # Cambodia
Link Asia/Bangkok Asia/Vientiane    # Laos

So the -402 timezone should be used for all dates before 1/4/1920, but it seems the implementation is using the -402 offset only from 1/1/1900 0:00:00.000 UTC (from 1/1/1900 6:42:04.000 in your timezone) and until 1/4/1920 in your timezone and -420 otherwise. I am not sure, if that is intended or a bug.

How can I ignore different time offset in same Timezone.

If you are actually using timezones in your application, then you should not ignore them. However, if you are making an application that is intended to be used just in your local timezone, then you can use a DateTime class without timezone information, such as java.time.LocalDateTime. Also worth noting: even if these timezones would be correct, the historical dates might still be inaccurate, due to modern time rules being applied for all time (see below). So in the end it depends on what your use case is.

A date-time without a time-zone in the ISO-8601 calendar system. The ISO-8601 calendar system is the modern civil calendar system used today in most of the world. It is equivalent to the proleptic Gregorian calendar system, in which today's rules for leap years are applied for all time. For most applications written today, the ISO-8601 rules are entirely suitable. However, any application that makes use of historical dates, and requires them to be accurate will find the ISO-8601 approach unsuitable.

CodePudding user response:

java.util.Date and java.text.SimpleDateFormat are very old classes. Although they mostly work, they are difficult to use properly, especially where timezones are concerned.

Date.getTimezoneOffset is deprecated. Do not use deprecated methods.

The proper way to work with timezone rules is using the java.time, java.time.zone, and java.time.format packages:

ZoneId zone = ZoneId.systemDefault();

DateTimeFormatter dateFormatter =
    DateTimeFormatter.ofPattern("MM/dd/yyyy H:mm:ss.SSS");

LocalDateTime date =
    LocalDateTime.parse("01/01/1900 7:00:00.000", dateFormatter);

System.out.println(date);
System.out.println(zone.getRules().getOffset(date));

LocalDateTime date2 =
    LocalDateTime.parse("01/01/1900 6:00:00.000", dateFormatter);

System.out.println(date2);
System.out.println(zone.getRules().getOffset(date2));

The entire history of a timezone is in the ZoneRules:

System.out.println();
zone.getRules().getTransitions().forEach(System.out::println);
System.out.println();
zone.getRules().getTransitionRules().forEach(System.out::println);
  • Related