-2

I need to output ZonedDateTime according to the locale I get later. Is there any way I can convert the ZonedDateTime value to the required format?

ZonedDateTime creationDate=ZonedDateTime.now();
//convert creationDate depending on the existing locale
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Ckafff
  • 3
  • 2
  • Yes, there is... What have you tried so far to output it? What format is the required one? – deHaar Sep 10 '20 at 12:39
  • @deHaar, the format is not particularly important, my teacher wants to see the time displayed as in the countries of the world if there is a locale – Ckafff Sep 10 '20 at 12:41
  • OK, then please provide something you tried yourself, it's mandatory here. Have a look at [the JavaDocs of `java.time.format.DateTimeFormatter`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html) for that. Maybe, you won't need an answer here after that anymore... – deHaar Sep 10 '20 at 12:45

1 Answers1

1

ZonedDateTime doesn't have a format. The concept of a ZonedDateTime is format-less.

Formatters are their own objects (instances of DateTimeFormatter). They are configurable (for example, you can change their locale, and all the locale-specific rendering they do, such as long-form month names, will then change), and you can ask them to format a provided zoneddatetime.

Thus, make your zoneddatetime object whenever you want, store it whereever you want. If, 3 days later, you need to format it according to some locale you just now got, great. Do that then:

ZonedDateTime zdt = ZonedDateTime.now();
// days pass

DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).localizedBy(Locale.FRENCH);

System.out.println(dtf.format(zdt));
rzwitserloot
  • 85,357
  • 5
  • 51
  • 72