Reputation: 2192
When I run the first segment, it is perfectly fine and generate an output. but in second case when I run this segment 2, It generates
DateTimeException : Unable to extract ZoneId from temporal.
segment 1:
LocalDate ld = LocalDate.now();
System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(ld));
segment 2:
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(dtf.format(ldt));
Upvotes: 3
Views: 10117
Reputation: 60036
I think It is a little complicated to explain, because you are mixing between two things ofLocalizedDate
, ofLocalizedDateTime
and the FormatStyle
:
In the first case you are calling ofLocalizedDate
with the FormatStyle.FULL
so you are ignoring the time part.
In the second case you are calling ofLocalizedDateTime
also with FormatStyle.FULL
which will include all the parts of the date, which is not the case for LocalDate
or LocalDateTime
.
To be sure lets try with MEDIUM
, or SHORT
instead of FULL
:
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(ldt)
=> 30 déc. 2019 à 14:57:40 - without any exception
For more details check the comments here :
/**
* Full text style, with the most detail.
* For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.
*/
FULL,
/**
* Long text style, with lots of detail.
* For example, the format might be 'January 12, 1952'.
*/
LONG,
/**
* Medium text style, with some detail.
* For example, the format might be 'Jan 12, 1952'.
*/
MEDIUM,
/**
* Short text style, typically numeric.
* For example, the format might be '12.13.52' or '3:30pm'.
*/
SHORT;
To resume we can create a this table :
ofLocalizedTime | ofLocalizedDate | ofLocalizedDateTime | |
---|---|---|---|
LocalTime | MEDIUM, SHORT | ||
LocalDate | FULL, LONG, MEDIUM, SHORT | ||
LocalDateTime | MEDIUM, SHORT | FULL, LONG, MEDIUM, SHORT | MEDIUM, SHORT |
ZonedDateTime | FULL, LONG, MEDIUM, SHORT | FULL, LONG, MEDIUM, SHORT | FULL, LONG, MEDIUM, SHORT |
OffsetDateTime | MEDIUM, SHORT | FULL, LONG, MEDIUM, SHORT | MEDIUM, SHORT |
=> FULL, LONG, MEDIUM, SHORT are FormatStyle
You can read it as LocalDateTime
can use ofLocalizedDate
with all the format styles, and no can't accept any FormatStyle
with ofLocalizedDateTime
Upvotes: 15
Reputation: 159135
You are confusing "localized" and "local":
ofLocalizedDateTime
: Returns a locale specific date-time formatter
LocalDateTime
: A date-time without a time-zone
As you can see, they are two entirely different terms.
Now, try supplying a ZonedDateTime
value, so you can see why it wants a ZoneId
.
ZonedDateTime zdt = ZonedDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
System.out.println(dtf.format(zdt));
Output (locale: en_US
, time zone: America/New_York
)
Monday, December 30, 2019 at 8:09:16 AM Eastern Standard Time
As you can see, it needs the time zone to know that the time is "Eastern Standard Time".
If you reduce the time style from FULL
to MEDIUM
, the time zone is no longer needed.
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM);
System.out.println(dtf.format(ldt));
Output
Monday, December 30, 2019, 8:09:16 AM
Upvotes: 3