Recipe Name:
Convert parseMutableDateTime to java.time
Description:
Convert parseMutableDateTime to java.time
Level:
error
Language:
  • java
Tags:
  • framework specific
  • java.time
  • Joda-Time
  • quality
Documentation

Migrate from org.joda.time.DateTimeFormat to Java Time

Joda-Time DateTimeFormat and ISODateTimeFormat migrate to java.time DateTimeFormatter.

Joda-time and java.time use same concept for DateTimeFormatters

There are 4 predefined types of time, they are:

  • Full
  • Long
  • Medium
  • Short

For parsing, none method has equivalent in java.time.

Examples

Before
    DateTimeFormatter joda = DateTimeFormat.fullTime();
    String jodaResult = joda.print(long);
After
    java.time.format.DateTimeFormatter javaFormatter = java.time.format.DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
    String javaResult = javaFormatter.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(long), ZoneId.systemDefault()));
References
Recipe
id: scw:java.time:Joda-Time:datetimeformatter-parseMutableDateTime
version: 10
metadata:
  name: Convert parseMutableDateTime to java.time
  shortDescription: Convert parseMutableDateTime to java.time
  level: error
  language: java
  enabled: true
  comment: "Searches for a parseMutableDateTime method called and provides fixes to migrate to a java.time equivalent.\n\nThis recipe is designed to match on broken code. This method originally was called by org.joda.time.format.DateTimeFormatter type, \nhowever this object type should first be migrated to java.time.format.DateTimeFormatter using the other migration recipes.\nThis recipe will then match on the broken code, and the fixes in this recipe will allow the completion of the migration.\nBe aware if you are using the correct pattern for this formatter."
  descriptionFile: Java/JodaTimeToJavaTime/DateTimeFormat/descriptions/datetimeformat-time.html
  tags: framework specific;java.time;Joda-Time;quality
search:
  methodcall:
    name: parseMutableDateTime
    type: java.time.format.DateTimeFormatter
availableFixes:
- name: Rewrite to ZonedDateTime.parse(string, dateTimeFormatter)
  actions:
  - modifyAssignedVariable:
      type: java.time.ZonedDateTime
  - rewrite:
      to: java.time.ZonedDateTime.parse({{{ arguments }}}, {{{ qualifier }}})
- name: Rewrite to OffsetDateTime.parse(string, dateTimeFormatter)
  actions:
  - modifyAssignedVariable:
      type: java.time.OffsetDateTime
  - rewrite:
      to: java.time.OffsetDateTime.parse({{{ arguments }}}, {{{ qualifier }}})