Recipe Name:
Convert parseLocalDateTime to java.time version
Description:
Convert parseLocalDateTime to java.time version
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 dateTime, they are:

  • Full
  • Long
  • Medium
  • Short

For parsing, only medium and short methods have equivalents in java.time.

Be aware if you are using the correct pattern for this formatter.

E.g. Using a date only formatter to try parse datetime.

Examples

Format

Before
    DateTimeFormatter joda = DateTimeFormat.longDateTime();
    String jodaResult = joda.print(long);
After
    java.time.format.DateTimeFormatter javaFormatter = java.time.format.DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
    String javaResult = javaFormatter.format(ZonedDateTime.ofInstant(Instant.ofEpochMilli(long), ZoneId.systemDefault()));

Parse

Before
    DateTimeFormatter jodaDtf = DateTimeFormat.mediumDateTime();
    long jodaResult = jodaDtf.parseMillis(expectedString);
After
    java.time.format.DateTimeFormatter javaDtf = java.time.format.DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    long javaResult =  java.time.LocalDateTime.parse(expectedString, javaDtf).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
References
Recipe
id: scw:java.time:Joda-Time:datetimeformatter-parseLocalDateTime
version: 10
metadata:
  name: Convert parseLocalDateTime to java.time version
  shortDescription: Convert parseLocalDateTime to java.time version
  level: error
  language: java
  enabled: true
  comment: "Searches for a parseLocalDateTime 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-datetime.html
  tags: framework specific;java.time;Joda-Time;quality
search:
  methodcall:
    name: parseLocalDateTime
    type: java.time.format.DateTimeFormatter
availableFixes:
- name: Rewrite parseLocalDateTime to java.time equivalent
  actions:
  - rewrite:
      to: java.time.LocalDateTime.parse({{{ arguments}}}, {{{ qualifier }}})
  - modifyAssignedVariable:
      type: java.time.LocalDateTime