Search code examples
javadatetimezonejodatimedst

Converting from java.util.TimeZone to org.joda.DateTimeZone


How is it possible in Java to convert an instance of java.util.TimeZone to org.joda.DateTimeZone and keeping the daylight saving time?


Solution

  • Joda-Time in maintenance-mode

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    java.time.ZoneId

    The modern replacement for java.util.TimeZone is java.time.ZoneId & java.time.ZoneOffset.

    You should avoid the old legacy date-time classes. But if necessary, you can convert to/from the java.time types. Look to new methods added to the old classes. You can move between TimeZone and ZoneId.

    java.util.TimeZone tz = java.util.TimeZone.getTimeZone( myZoneId );
    

    …and…

    java.time.ZoneId z = myLegacyTimeZone.toZoneId();
    

    If you are looking for the offset-from-UTC or Daylight Saving Time (DST) info for the zone, look at the ZoneRules class. Search Stack Overflow for more discussion and examples on that, or edit your Question to describe more about your goal.