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

  • Full
  • Long
  • Medium
  • Short

For parsing, all 4 methods have equivalents in java.time.

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

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

Examples

Format

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

Parse

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