Recipe Name:
Rewrite getYearOfCentury() to java.time equivalent
Description:
Rewrite getYearOfCentury() to java.time equivalent
Level:
warning
Language:
  • java
Tags:
  • java.time
  • framework specific
  • Joda-Time
  • quality
Documentation

getYearOfCentury() is not available in java.time

The getYearOfCentury() method is no longer supported after migrating from Joda-Time to java.time

This recipe will replace the getYearOfCentury method call, with an equivalent calculation which is based on the logic used in Joda-Time to calculate year of century.

java.lang.Math.abs( % 100)
Recipe
id: scw:java.time:Joda-Time:getYearOfCentury
version: 10
metadata:
  name: Rewrite getYearOfCentury() to java.time equivalent
  shortDescription: Rewrite getYearOfCentury() to java.time equivalent
  level: warning
  language: java
  enabled: true
  comment: |-
    Joda-Time provided a getYearOfCentury() method to access the year-of-century value of a Date-related object.
    This method is no longer provided in java.time, however you can calculate the equivalent result with a simple equation.
    This recipe provides a fix to rewrite the getYearOfCentury() method call to the equivalent equation.

    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/getYearOfCentury.html
  tags: java.time;framework specific;Joda-Time;quality
search:
  methodcall:
    name: getYearOfCentury
    anyOf:
    - type: java.time.ZonedDateTime
    - type: java.time.OffsetDateTime
    - type: java.time.LocalDateTime
    - type: java.time.LocalDate
availableFixes:
- name: Rewrite getYearOfCentury() to Math.abs(getYear() % 100)
  actions:
  - rewrite:
      to: java.lang.Math.abs({{{ qualifier }}}.getYear() % 100)