Recipe Name:
Rewrite getDateTimeField method to get(ChronoField.DATE_TIME_FIELD)
Description:
Rewrite getDateTimeField method to get(ChronoField.DATE_TIME_FIELD)
Level:
warning
Language:
  • java
Tags:
  • java.time
  • framework specific
  • Joda-Time
  • quality
Documentation

get() method is no longer supported

Joda-Time provided some convenience methods to access Date/Time fields using a get method

java.time has a smaller set of these convenience methods, so the following are no longer available:

  • getEra
  • getMillisOfDay
  • getMillisOfSecond
  • getMinuteOfDay
  • getSecondOfDay
  • getYearOfEra

There is an equivalent method of retreiving these field values, by using the get(TemporalField field) method. For example

int milliOfDay = myDateTime.get(ChronoField.MILLI_OF_DAY)
Recipe
id: scw:java.time:Joda-Time:getChronoField
version: 10
metadata:
  name: Rewrite getDateTimeField method to get(ChronoField.DATE_TIME_FIELD)
  shortDescription: Rewrite getDateTimeField method to get(ChronoField.DATE_TIME_FIELD)
  level: warning
  language: java
  enabled: true
  comment: |-
    Joda-Time provided convenience get methods for some less common date and time fields.
    In java.time these methods have been removed but the field values can still be accessed using the get(ChronoField) method.
    This recipe provides fixes to transform from the previous method names to the new get(ChronoField) methods.

    This recipe is designed to match on broken code as part of an overall migration from Joda-Time to java.time.
    After an Joda-Time object has been migrated to a java.time equivalent, some subsequent method calls may become invalid as they are no longer provided by java.time.
    This recipe is designed to match on one of those method calls and provide a fix.
  descriptionFile: descriptions/getChronoField.html
  tags: java.time;framework specific;Joda-Time;quality
search:
  methodcall:
    allOf:
    - anyOf:
      - name: getEra
      - name: getMinuteOfDay
      - name: getSecondOfDay
      - name: getYearOfEra
      - name: getMillisOfSecond
      - name: getMillisOfDay
    - anyOf:
      - type: java.time.ZonedDateTime
      - type: java.time.OffsetDateTime
      - type: java.time.LocalDateTime
      - type: java.time.LocalDate
      - type: java.time.LocalTime
availableFixes:
- name: Rewrite to get(ChronoField.{DATE_TIME_FIELD})
  actions:
  - rewrite:
      to: '{{{ qualifier }}}.get(java.time.temporal.ChronoField.{{#upperCase}}{{#camelToUnderscoreCase}}{{#sed}}s/Millis/Milli/,{{#sed}}s/get//,{{{ methodName }}}{{/sed}}{{/sed}}{{/camelToUnderscoreCase}}{{/upperCase}})'