Recipe Name:
Migrate new DateTime(year, month, day, hour, minute, second, millis) to java.time
Description:
Migrate new DateTime(year, month, day, hour, minute, second, millis) 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-year-month-day-hour-minute-second-millis
version: 10
metadata:
  name: Migrate new DateTime(year, month, day, hour, minute, second, millis) to java.time
  shortDescription: Migrate new DateTime(year, month, day, hour, minute, second, millis) 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:
        anyOf:
        - type: int
        - type: java.lang.Integer
      2:
        anyOf:
        - type: int
        - type: java.lang.Integer
      3:
        anyOf:
        - type: int
        - type: java.lang.Integer
      4:
        anyOf:
        - type: int
        - type: java.lang.Integer
      5:
        anyOf:
        - type: int
        - type: java.lang.Integer
      6:
        anyOf:
        - type: int
        - type: java.lang.Integer
      7:
        anyOf:
        - type: int
        - type: java.lang.Integer
    argCount: 7
    type: org.joda.time.DateTime
availableFixes:
- name: Migrate to java.time.ZonedDateTime
  actions:
  - rewrite:
      to: java.time.ZonedDateTime.of({{{ arguments.0 }}}, {{{ arguments.1 }}}, {{{ arguments.2 }}}, {{{ arguments.3 }}}, {{{ arguments.4 }}}, {{{ arguments.5 }}}, 0, java.time.ZoneId.systemDefault()).with(java.time.temporal.ChronoField.MILLI_OF_SECOND, {{{ arguments.6 }}})
      target: self
  - modifyAssignedVariable:
      type: java.time.ZonedDateTime