1

I working on convertion Grails Joda-Time plugin to JavaTime.

And I've old Joda time code like this:

    def style
    switch (type) {
        case LocalTime:
            style = '-S'
            break
        case LocalDate:
            style = 'S-'
            break
        default:
            style = 'SS'
    }
    Locale locale = LocaleContextHolder.locale
    return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT)

How can I conver it to JSR 310? I can't find anything similar to method forStyle(String style) which accepts style.

UPD I found workaround:

        Locale locale = LocaleContextHolder.locale
        DateTimeFormatter formatter
        switch (type) {
            case LocalTime:
                formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
                break
            case LocalDate:
                formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(locale)
                break
            default:
                formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(locale)
        }
        return formatter

But it fails of Instant type. Spock spec to reproduce:

def 'Instant locale formatting'() {
    given:
    Instant inst = Instant.ofEpochMilli(92554380000L)
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(UK)
    expect:
    formatter.format(inst) == "07/12/72 05:33"
}

This test fails with error:

java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
    at java.time.Instant.getLong(Instant.java:603)
    at java.time.format.DateTimePrintContext$1.getLong(DateTimePrintContext.java:205)
    at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
    at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2543)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
    at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4350)
    at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182)
    at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1744)
    at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1718)

So, why formatter can't format Instant?

Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43

2 Answers2

7

The methods ofLocalizedDate(), ofLocalizedTime() and ofLocalizedDateTime() provide the localized formats.

To format an Instant a time-zone is required. This can be added to the formatter using withZone():

DateTimeFormatter formatter =
    DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
                     .withLocale(UK)
                     .withZone(ZoneId.systemDefault());

Without the zone, JSR-310 formatter has no knowledge of how to convert the instant to human date-time fields.

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
  • 1
    I found a strange issue, possible a bug http://stackoverflow.com/questions/27488494/how-to-parse-string-with-date-but-without-time-in-local-format-to-zoneddatetime – Sergey Ponomarev Dec 16 '14 at 13:58
1

JSR 310 has other ways to replace style: DateTimeFormatter.ofLocalizedTime(), DateTimeFormatter.ofLocalizedDate() and DateTimeFormatter.ofLocalizedDateTime()

Other problem is that Instant type can't be formatted Format Instant to String

Community
  • 1
  • 1
Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43