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

getCenturyOfEra() is not available in java.time

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

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

java.lang.Math.abs( / 100)
Recipe
id: scw:java.time:Joda-Time:getCenturyOfEra
version: 10
metadata:
  name: Rewrite getCenturyOfEra to java.time equivalent
  shortDescription: Rewrite getCenturyOfEra to java.time equivalent
  level: warning
  language: java
  enabled: true
  comment: |-
    Joda-Time provided a getCenturyOfEra() method to access the century of era 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 getCenturyOfEra() 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/getCenturyOfEra.html
  tags: java.time;framework specific;Joda-Time;quality
search:
  methodcall:
    name: getCenturyOfEra
    anyOf:
    - type: java.time.ZonedDateTime
    - type: java.time.OffsetDateTime
    - type: java.time.LocalDateTime
    - type: java.time.LocalDate
availableFixes:
- name: Rewrite getCenturyOfEra() to Math.abs(year / 100)
  actions:
  - rewrite:
      to: java.lang.Math.abs({{{ qualifier }}}.getYear() / 100)