Recipe Name:
Rewrite toYearMonthDay() to toLocalDate()
Description:
Rewrite toYearMonthDay() to toLocalDate()
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-toYearMonthDay
version: 10
metadata:
  name: Rewrite toYearMonthDay() to toLocalDate()
  shortDescription: Rewrite toYearMonthDay() to toLocalDate()
  level: warning
  language: java
  enabled: true
  comment: |
    Joda-Time originally included a 'YearMonthDay' class which has been deprecated in favour of LocalDate.
    The DateTime class had a deprecated method toYearMonthDay() to create a YearMonthDay object from an existing DateTime.
    This recipe is supplied to provide a migration in the case where the code-base is still using this deprecated method.
    There is no YearMonthDay class in java.time, however the concept is represented by LocalDate, so we can use toLocalDate() to achieve the same result.

    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:
    argCount: 0
    name: toYearMonthDay
    anyOf:
    - type: java.time.ZonedDateTime
    - type: java.time.OffsetDateTime
availableFixes:
- name: Rewrite method call to java.time equivalent toLocalDate()
  actions:
  - rewrite:
      to: '{{{ qualifier }}}.toLocalDate()'
  - modifyAssignedVariable:
      type: java.time.LocalDate