- java
- framework specific
- java.time
- Joda-Time
- quality
Converting a Custom DateTime pattern from Joda-Time to java-time
The following terminology is used below to describe elements of a date time pattern:
- field - 1 or more of the same character in a Date/Time Format pattern
- width - the number of characters in a field
Changes to format characters
Some formatting characters have changed from Joda-Time to java.time
Field Name | Joda-Time Character | java.time Character |
---|---|---|
Year of Era | Y | y |
Week-based year | x | Y |
year | y | u |
Maximum width in java.time
java.time is much stricter about the maximum width of a field. The Maximum width is listed below where it is applicable. Joda-Time would safely handle blocks that were longer than the maximum width, whereas java.time will throw an exception.
- For number values, Joda-Time would simply pad the value using 0's. You can replicate this behaviour by adding a literal field of Zeros in front of the maximum width. For example if your pattern in Joda Time was DDDDD, and the new maximum width is 3, then a java.time pattern to keep the same output would be '00'DDD.
- For text values, Joda-Time would choose the full form for any fields with width 4 or more. java.time is much more specific. It will often have an additional version for width 5, and throw an exception for anything larger than 5.
Field Name | Joda Character | java.time Character | Maximum Width |
---|---|---|---|
Week of Week-based year | w | w | 3 |
Day of Week (Number) | e | e | 2 |
Day of Year (Number) | D | D | 3 |
Month of Year | M | M | 4 |
day of Month | d | d | 2 |
Half-day of day | a | a | 1 |
Hour of Half-day | K | K | 2 |
Clockhour of Half-day | h | h | 2 |
Hour of day (0-23) | H | H | 2 |
Clockhour of Day (1-24) | k | k | 2 |
Minute of Hour | m | m | 2 |
Second of Minute | s | s | 2 |
Time Zone | z | z | 4 |
First Day of Week is now Locale Dependant
Joda-Time considered Monday to be the first day of the week, in accordance with the ISO8601 standard. java.time is different in that the first day of the week is dependent on the locale. This means the output of following fields will be different in java.time if your locale considers Sunday to be the first day of the week
If you need to ensure the output is the same as Joda-Time you will need to make sure your formatter uses a locale that considers Monday to be the first day of the week.
Field | Joda-Time Character | java.time character |
---|---|---|
Week-based year | x | y |
Week of Week-based year | w | w |
Day of week (Number) | e | e |
Century of Era [C] no longer supported
Century Of Era is not supported in java.time. You will need to remove C fields from your pattern.
G - Era
In order to keep the same output as Joda-Time, Era field should be converted to a single width G. Joda-Time only had 1 length for Era, so if you specified 'G' or 'GGGGGGGG' you would always get the same output. In java.time, there are multiple forms e.g. AD, Anno Domini, A If you want to match exactly the same output as you did in Joda-Time, you should convert all you G to single width.
E - Day of Week Text
java.time has a new formatting option for EEEEE, which is different to EEEE. If you want to keep the same output as your Joda-Time pattern, you should replace any 5 or more occurances to 4.
S - fraction of second
The measurement precision of fraction of a second has increased from milliseconds to nanoseconds. If you were using a block of 4 or more [SSSS, SSSSS, ...], Joda would pad the 4th digit onwards with 0, due to the precision stopping at milliseconds. e.g. [1230, 12300, ...] java.time will now output up to 9 digits of precision for fraction of a second due to it's enhanced precision to nanoseconds. e.g. [1234, 12345, ....]
Z - time zone offset/id
In Joda-Time Time Zone Offset and Id were represented by the same character Z
In java.time Time Zone Offset remains as Z, however Time Zone Id has a new character V.
- Z - Same
- ZZ - Convert to ZZZZZ
- ZZZ and wider - Convert to VV
id: scw:java.time:Joda-Time:datetimeformat-forPattern version: 10 metadata: name: Convert Joda-Time DateTimeFormat.forPattern to java.time.format.DateTimeFormatter.ofPattern shortDescription: Convert Joda-Time DateTimeFormat.forPattern to java.time.format.DateTimeFormatter.ofPattern level: warning language: java enabled: true comment: Searches for org.joda.time.format.DateTimeFormat.forPattern method call and provides fixes to migrate to a java.time equivalent. descriptionFile: descriptions/datetimeformat-forpattern.html tags: framework specific;java.time;Joda-Time;quality search: methodcall: name: forPattern type: org.joda.time.format.DateTimeFormat availableFixes: - name: Convert to java.time.format.DateTimeFormatter actions: - rewrite: to: java.time.format.DateTimeFormatter.ofPattern({{{ arguments }}}) - modifyAssignedVariable: type: java.time.format.DateTimeFormatter