Recipe Name:
Convert DateTimeZone.getName to java.time equivalent
Description:
Convert DateTimeZone.getName to java.time equivalent
Level:
warning
Language:
  • java
Tags:
  • java.time
  • framework specific
  • 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:datetimezone-getName-locale
version: 10
metadata:
  name: Convert DateTimeZone.getName to java.time equivalent
  shortDescription: Convert DateTimeZone.getName to java.time equivalent
  level: warning
  language: java
  enabled: true
  comment: |-
    This recipe is designed to match on broken code. The method that is being searched for does not actually exist on java.time.ZoneId.

    After migrating an org.joda.time.DateTimeZone instance to a java.time.ZoneId, subsequent calls may be made to methods that existed for DateTimeZone, but no longer exist for ZoneId.
    This recipe matches on the methodname that no longer exists, and suggests an equivalent rewrite that will work for java.time.
  descriptionFile: descriptions/datetimezone.html
  tags: java.time;framework specific;Joda-Time;quality
search:
  methodcall:
    args:
      2:
        type: java.util.Locale
    argCount: 2
    name: getName
    type: java.time.ZoneId
availableFixes:
- name: Rewrite to Instant.ofEpochMilli(long).atZone(zoneId).format()
  actions:
  - rewrite:
      to: java.time.Instant.ofEpochMilli({{{ arguments.0 }}}).atZone({{{ qualifier }}}).format(java.time.format.DateTimeFormatter.ofPattern("zzzz",{{{ arguments.1 }}}));