Recipe Name:
Remove obsolete toDateTime method call
Description:
Remove obsolete toDateTime method call
Level:
warning
Language:
  • java
Tags:
  • java.time
  • framework specific
  • 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-toDateTime
version: 10
metadata:
  name: Remove obsolete toDateTime method call
  shortDescription: Remove obsolete toDateTime method call
  level: warning
  language: java
  enabled: true
  comment: |
    Joda-Time's DateTime class provided a toDateTime() method which simply returned
    'this'. java.time does not have a toDateTime() method but we can provide an
    equivalent by simply removing the method call.
    Additionally a toMutableDateTime() method was available which performed a
    similar operation, returning a MutableDateTime instead. Since MutableDateTime is
    not available in java.time, the closest migration is to switch to the immutable
    objects available in java.time. Any subsequent attempted mutable operations will
    need to be migrated separately after using this recipe.

    This recipe is designed to match on broken code as part of an overall Joda-Time
    to java.time migration. The method that is being searched for does not actually
    exist on ZoneDateTime/OffsetDateTime. After migrating a DateTime instance to a
    ZoneDateTime or OffsetDateTime, subsequent calls may be made to methods that
    existed for DateTime, but no longer exist for ZonedDateTime/OffsetDateTime.
    This recipe matches on the methodname that no longer exists, and suggests an
    equivalent rewrite that will work for java.time.
  descriptionFile: descriptions/datetime.html
  tags: java.time;framework specific;Joda-Time;quality
search:
  methodcall:
    allOf:
    - anyOf:
      - name: toDateTime
      - name: toDateTimeISO
      - name: toMutableDateTime
    - anyOf:
      - type: java.time.ZonedDateTime
      - type: java.time.OffsetDateTime
    argCount: 0
availableFixes:
- name: Remove obsolete toDateTime() method call
  actions:
  - rewrite:
      to: '{{{ qualifier }}}'
  - modifyAssignedVariable:
      type: '{{{ qualifier.type }}}'