The call ofLocalizedDateTime()
can be deleted because at the end you call ofPattern()
, create a different formatter with a completely different template (and the template returned ofLocalizedDateTime(FormatStyle.FULL)
is very different from month year
, so this is not exactly what you want).
: Mayo
- , MMMM
( javadoc ). , DateTimeFormatter
( , , ), , .
, java.time.format.DateTimeFormatterBuilder
:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("MMMM yyyy")
.toFormatter(new Locale("es", "ES"));
fmt.parse("Mayo 2017");
, java.time.YearMonth
, ( ):
YearMonth ym = YearMonth.parse("Mayo 2017", fmt);
System.out.println(ym);
, SimpleDateFormat
. , Date
/, 1 ( JVM ).
API , . - parseDefaulting
java.time.temporal.ChronoField
:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// case insensitive
.parseCaseInsensitive()
// pattern with full month name (MMMM)
.appendPattern("MMMM yyyy")
// default value for day of month
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
// default value for hour
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
// default value for minute
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
// set locale
.toFormatter(new Locale("es", "ES"));
LocalDateTime
, :
LocalDateTime dt = LocalDateTime.parse("Mayo 2017", fmt);
System.out.println(dt);
java.util.Date
, SimpleDateFormat
, LocalDateTime
JVM Date
:
Date javaUtilDate = Date.from(dt.atZone(ZoneId.systemDefault()).toInstant());
, JVM (ZoneId.systemDefault()
), , SimpleDateFormat
.
- YearMonth
:
// in this case, the formatter doesn't need the default values
YearMonth ym = YearMonth.parse("Mayo 2017", fmt);
ZonedDateTime z = ym
// set day of month to 1
.atDay(1)
// midnight at JVM default timezone
.atStartOfDay(ZoneId.systemDefault());
Date javaUtilDate = date.from(z.toInstant());
, , .
API IANA ( Region/City
, America/New_York
Europe/Berlin
), ZoneId.of("America/New_York")
, .
(, CST
PST
), .
( , ), ZoneId.getAvailableZoneIds()
.