MST is mapped to the Denver time zone in joda, which is currently the MDT. Is this a bug in joda DateTimeZone?

I need to convert GMTto Arizona. Currently in Arizona there is MST(Daylight Saving Time).

However, in the code, Joda Time DateTimeZone MSTwas mapped to America/Denver:

map.put("MST", "America/Denver");

You can see that currently Denver has a daylight saving time and therefore has MDT. Then why was this mapping done in DateTimeZonecode?

When converting to a local time zone for Arizona from GMT, I get an offset as GMT-6, which is incorrect and should have been GMT-7since it MSTdoes not have daylight saving time.

This is mistake? How to solve the problem?

+4
source share
2 answers

At first, I thought that Joda time would use the same TimeZone identifiers as the underlying Java TimeZone. I debugged the code and found that it was a bit more complicated. Although it can be parallel with Joda using the same identifiers as Java, it actually loads time zone information from files in the joda jar. This example runs with all Joda classes. dt came out as having a phoenix, arizona time.

public static void main(String[] args)
{
  //List all time zones
  Set<String> timezones = DateTimeZone.getAvailableIDs();
  for(String tz : timezones)
  {
    System.out.println(tz);
  }

  DateTimeZone arizona = DateTimeZone.forID("US/Arizona");
  DateTimeZone.setDefault(arizona);
  DateTime dt = new DateTime();
  System.out.println(dt);

}
+1
source

The problem with these 3 letter abbreviations is that they are ambiguous, not standard .

, MST - , , DST ( DST, , - DST , ).

API (java.util.TimeZone) , , MST America/Denver, joda-time - IMO - ( - jodatime 2.7). , :

map.put("MST", "America/Denver");  // JDK 1.1 compatible

DateTimeZone.forTimeZone(TimeZone), TimeZone jodatime DateTimeZone ( ).


, , , @Ole V.V. America/Phoenix MST. , .

(Continent/City) IANA database , Java API Joda.

:

// create a date/time in UTC
DateTime utc = new DateTime("2017-03-26T10:00:00Z", DateTimeZone.UTC);
System.out.println(utc); // 2017-03-26T10:00:00.000Z

// convert to Arizona timezone
DateTimeZone arizona = DateTimeZone.forID("America/Phoenix");
DateTime arizonaDate = utc.withZone(arizona);
System.out.println(arizonaDate); // 2017-03-26T03:00:00.000-07:00

API Java Time

, API Date/Time ( Joda- API , , , ).

joda , joda website : , Joda-Time "" , . Java SE 8, java.time(JSR-310).

Java 8, API java.time. , , API.

Java <= 7, ThreeTen Backport, backport Java 8 . Android ThreeTenABP ( , ).

. - ( Java 8 - java.time, ThreeTen Backport ( Android ThreeTenABP) - org.threeten.bp), .

API MST , Map:

// custom map with timezone names
Map<String, String> map = new HashMap<>();
// map "MST" to Arizona timezone
map.put("MST", "America/Phoenix");
// create timezone
ZoneId arizona = ZoneId.of("MST", map);

// datetime in UTC
ZonedDateTime utc = ZonedDateTime.parse("2017-03-26T10:00:00Z");
System.out.println(utc); // 2017-03-26T10:00Z

// convert to Arizona timezone
ZonedDateTime arizonaDate = utc.withZoneSameInstant(arizona);
System.out.println(arizonaDate); // 2017-03-26T03:00-07:00[America/Phoenix]

, ZoneId.of("America/Phoenix"), MST, .

, API []. , DateTimeFormatter. ( javadoc ) :

// use built-in formatter
DateTimeFormatter fmt = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
System.out.println(fmt.format(arizonaDate)); // 2017-03-26T03:00:00-07:00

// use custom format
fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
System.out.println(fmt.format(arizonaDate)); // 2017-03-26T03:00:00.000-07:00
+1

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


All Articles