Recipe Name:
Convert Joda-Time DateTimeFormat.fullDateTime()
Description:
Convert Joda-Time DateTimeFormat.fullDateTime()
Level:
warning
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:datetime-format-fullDateTime
version: 10
metadata:
  name: Convert Joda-Time DateTimeFormat.fullDateTime()
  shortDescription: Convert Joda-Time DateTimeFormat.fullDateTime()
  level: warning
  language: java
  enabled: true
  comment: Searches for org.joda.time.format.DateTimeFormat.fullDateTime method call and provides fixes to migrate to a java.time equivalent.
  descriptionFile: descriptions/datetimeformat-datetime.html
  tags: framework specific;java.time;Joda-Time;quality
search:
  methodcall:
    name: fullDateTime
    type: org.joda.time.format.DateTimeFormat
availableFixes:
- name: Convert to java.time DateTimeFormatter
  actions:
  - rewrite:
      to: java.time.format.DateTimeFormatter.ofLocalizedDateTime(java.time.format.FormatStyle.FULL)
  - modifyAssignedVariable:
      type: java.time.format.DateTimeFormatter