Recipe Name:
Migrate new DateTime(Date, DateTimeZone) to java.time
Description:
Migrate new DateTime(Date, DateTimeZone) to java.time
Level:
warning
Language:
  • java
Tags:
  • framework specific
  • java.time
  • Joda-Time
  • quality
Documentation

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

Joda-Time DateTime migrates to java.time ZonedDateTime or OffsetDateTime.

ZonedDateTime is the same concept, date and time with time-zone.

OffsetDateTime is a new concept, date and time with offset from UTC.

Before
    DateTime dateTime = new DateTime();
    int monthOfYear = dateTime.getMonthOfYear();
After
    ZonedDateTime dateTime = ZonedDateTime.now();
    int monthOfYear = dateTime.getMonthValue();
References
Recipe
id: scw:java.time:Joda-Time:DateTime-constructor-Date-DateTimeZone
version: 10
metadata:
  name: Migrate new DateTime(Date, DateTimeZone) to java.time
  shortDescription: Migrate new DateTime(Date, DateTimeZone) to java.time
  level: warning
  language: java
  enabled: true
  comment: |
    Searches for an instance creation of org.joda.time.DateTime and provides fixes to migrate to a java.time equivalent.

    This recipe will cause broken code as part of an overall number of steps to perform a migration of Joda-Time to java.time.

    After migrating this instance creation to the java.time equivalent, subsequent method calls and usages of the instance may become invalid.
    Further Sensei recipes are available to help resolve many of these invalid usages.
  descriptionFile: descriptions/datetime.html
  tags: framework specific;java.time;Joda-Time;quality
search:
  instanceCreation:
    args:
      1:
        type: java.util.Date
      2:
        anyOf:
        - type: java.time.ZoneId
        - type: org.joda.time.DateTimeZone
    argCount: 2
    type: org.joda.time.DateTime
availableFixes:
- name: Migrate to java.time ZonedDateTime
  actions:
  - rewrite:
      to: java.time.ZonedDateTime.ofInstant({{{ arguments.0 }}}.toInstant(), {{{ arguments.1 }}})
  - modifyAssignedVariable:
      type: java.time.ZonedDateTime
- name: Migrate to java.time.OffsetDateTime
  actions:
  - rewrite:
      to: java.time.OffsetDateTime.ofInstant({{{ arguments.0 }}}.toInstant(), {{{ arguments.1 }}})
  - modifyAssignedVariable:
      type: java.time.OffsetDateTime