Recipe Name:
Rewrite minus millis method call to java.time equivalent
Description:
Rewrite minus millis method call to java.time equivalent
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-minus-long
version: 10
metadata:
  name: Rewrite minus millis method call to java.time equivalent
  shortDescription: Rewrite minus millis method call to java.time equivalent
  level: warning
  language: java
  enabled: true
  comment: |
    Joda-Time's DateTime class provided methods to create a new DateTime object by subtracting milliseconds from an existing DateTime object.
    These methods are not available in java.time, but you can achieve the same result by using minus(long, ChronoUnit.MILLIS)

    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: framework specific;java.time;Joda-Time;quality
search:
  methodcall:
    args:
      1:
        anyOf:
        - type: long
        - type: java.lang.Long
        - type: int
        - type: java.lang.Integer
    allOf:
    - anyOf:
      - name: minus
      - name: minusMillis
    - anyOf:
      - type: java.time.ZonedDateTime
      - type: java.time.OffsetDateTime
    argCount: 1
availableFixes:
- name: 'Rewrite to java.time equivalent: minus(value, ChronoUnit.MILLIS)'
  actions:
  - rewrite:
      to: '{{{ qualifier }}}.minus({{{ arguments.0 }}}, java.time.temporal.ChronoUnit.MILLIS)'
  - modifyAssignedVariable:
      type: '{{{ qualifier.type }}}'