2

Ok, I need some help to understand how to use the java.time formatters. I keep crashing on "Unsupported field" exceptions all day (pun not intended, sorry).

Just some examples.

This is a very simple basic FormatStyle formatting:

import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class JavaTimeFormat {
    public static void main(String[] args) {
        Instant now = Instant.now();
        DateTimeFormatter formatter =
            DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        System.out.println(formatter.format(now));
    }
}

All I get is an exception:

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException:
Unsupported field: DayOfMonth

Let's try with a simple pattern.

import java.time.Instant;
import java.time.format.DateTimeFormatter;

public class JavaTimeFormat {
    public static void main(String[] args) {
        Instant now = Instant.now();
        DateTimeFormatter formatter =
            DateTimeFormatter.ofPattern("dd/MM/yyyy");
        System.out.println(formatter.format(now));
    }
}

Same problem.

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException:
Unsupported field: DayOfMonth

I need a flexible way to solve this as I am trying to put together a JSTL formatting library for java.time forked from Joda Time JSP tags.

EDIT: I don't think it's exactly a duplicate of the other question. This is more general as I found the problem also with patterns. I give you that the solution answered there is the one that answers mine too. Anyway I've edited the title to make it easier to find this.

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
sargue
  • 5,695
  • 3
  • 28
  • 43

0 Answers0