Antonio Antonio - 1 year ago 247
Java Question

Java 8 parse ZonedDateTime from string not containing all fields

When I have only year (and possibly several other temporal fields) specified in a string pattern, I would like the other absent fields to be set to their lowest values. For example, prior to Java 8 my code looked like

String source = "2015";
String pattern = "yyyy";
DateFormat sdf = new SimpleDateFormat(pattern);
Date date = null;
try {
date = sdf.parse(source);
} catch (ParseException e) {

SimpleDateFormat sdfGMT2 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");

String newf = sdfGMT2.format(date);

which returns me
2015.01.01 00:00:00 GMT

However, in
formatter returns a
; the code below throws exception

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("YYYY");
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2008", fmt);


java.time.format.DateTimeParseException: Text '2008' could not be parsed: Unable to obtain
ZonedDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2008},ISO
of type java.time.format.Parsed

How do I achieve the same result with Java 8 time API as I did with older API?

Answer Source

It's a bit more verbose, but you have to specify the default values for missing fields when creating your DateTimeFormatter:

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
        .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
        .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)

ZonedDateTime zonedDateTime = ZonedDateTime.parse("2008", fmt);

This includes the ZoneId which is necessary for a ZonedDateTime.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download