Recipe Name:
Convert DateTimeZone[] to ZoneId[]
Description:
Convert DateTimeZone[] to ZoneId[]
Level:
warning
Language:
  • java
Tags:
  • framework specific
  • java.time
  • Joda-Time
  • quality
Documentation

Migrate from org.joda.time.DateTimeZone to Java Time

Joda-Time DateTimeZone migrates to java.time ZoneId or ZoneOffset.

ZoneId is the same concept, an identifier of a time-zone.

ZoneOffset is a new concept, an offset from UTC.

Before
    org.joda.time.DateTimeZone forID = DateTimeZone.forID(zoneId);
    org.joda.time.DateTimeZone forOffsetHours = DateTimeZone.forOffsetHours(offsetHours);
After
    ZoneId forID = ZoneId.of(zoneId);
    ZoneOffset forOffsetHours = ZoneOffset.ofHours(offsetHours);

Note: the recipes for getName and getShortName do not map exactly to the ZoneId.getName method, this is deliberate. The Joda-Time getName and getShortName methods will return different names depending on whether the long argument(which represents an instant in time) is in daylight savings time or not. In java.time, the ZoneId getName method does not respect daylight savings time changes. In order to follow this original behaviour, we need to create a ZonedDateTime and format it using a DateTimeFormatter, this is what the recipes have been designed to do.

References
Recipe
id: scw:java.time:Joda-Time:Convert_DateTimeZone_to_ZoneId-array
version: 10
metadata:
  name: Convert DateTimeZone[] to ZoneId[]
  shortDescription: Convert DateTimeZone[] to ZoneId[]
  level: warning
  language: java
  enabled: true
  comment: |-
    This recipe searches for a field that is declared as a DateTimeZone array and provides a fix to rewrite it to a java.time.ZoneId array.

    This may introduce further problem markers in the IDE, as your field may reference DateTimeZone methods that do not exist on java.time.ZoneId.
    Further sensei recipes are available to help migrate these invalid method names to java.time equivalents where possible.
  descriptionFile: descriptions/datetimezone.html
  tags: framework specific;java.time;Joda-Time;quality
search:
  field:
    type: org.joda.time.DateTimeZone[]
availableFixes:
- name: Change Arrays of DateTimeZone to ZoneId
  actions:
  - rewrite:
      to: '{{{ modifierList }}} java.time.ZoneId[] {{{ qualifier }}};'
      target: self