Recipe Name:
Constructor argument (Object) needs to be migrated to java.time before Constructor Migration can occur
Description:
Constructor argument (Object) needs to be migrated to java.time before Constructor Migration can occur
Level:
info
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-Object-DateTimeZone
version: 10
metadata:
  name: Constructor argument (Object) needs to be migrated to java.time before Constructor Migration can occur
  shortDescription: Constructor argument (Object) needs to be migrated to java.time before Constructor Migration can occur
  level: info
  language: java
  enabled: true
  comment: |
    Joda-Time provided a new DateTime(Object, DateTimeZone) constructor, which would accept any type of Object.
    Within the constructor, it would look up a list of converters to assist in converting this object to a DateTime.
    Additionally the converter list was configurable, so you could indeed write your own converter which might convert an object from your own problem domain into a DateTime

    In java.time, this converter mechanism no longer exists.
    The equivalent method ZonedDateTime.from(TemporalAccessor temporal) expects the object to implement the TemporalAccessor.
  descriptionFile: descriptions/datetime.html
  tags: framework specific;java.time;Joda-Time;quality
search:
  instanceCreation:
    args:
      1:
        not:
          anyOf:
          - type: long
          - type: java.lang.Long
          - type: int
          - type: java.lang.Integer
          - type: java.util.Calendar
          - type: java.util.Date
          - type: java.lang.String
          - type: java.time.temporal.TemporalAccessor
          - type: org.joda.time.ReadableInstant
      2:
        anyOf:
        - type: java.time.ZoneId
        - type: org.joda.time.DateTimeZone
    argCount: 2
    type: org.joda.time.DateTime
availableFixes: []