How do you set a date in yyyy-MM-dd format and then be able to print it out later on in the same format in Java 1.8?

Viewed 125

In the following code:

    ZonedDateTime zdt = ZonedDateTime.now();
    DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String zdtString = FORMATTER.format(zdt);
    System.out.println(zdtString);

you will see it prints out the current date in yyyy-DD-mm format. Since this question was posted on July 17, 2021, it printed:

2021-07-17

But now I would like to change the date to something different (like 1994-03-24).

So I tried:

    DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    ZonedDateTime zdt2 = ZonedDateTime.parse("1994-03-24", FORMATTER);
    String zdtString = FORMATTER.format(zdt2);
    System.out.println(zdtString);

But then I get the following Exception:

Exception in thread "main" java.time.format.DateTimeParseException: Text '1994-03-24' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
    at javaapplication5.JavaApplication5.main(JavaApplication5.java:48)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
    at java.time.ZonedDateTime.from(ZonedDateTime.java:565)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 2 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneId from TemporalAccessor: {},ISO resolved to 1994-03-24 of type java.time.format.Parsed
    at java.time.ZoneId.from(ZoneId.java:466)
    at java.time.ZonedDateTime.from(ZonedDateTime.java:553)
    ... 4 more

How do I set my own dates to something other than current date?

1 Answers

1994-03-24 does not have timezone information and therefore it can not be parsed into ZonedDateTime until you provide the timezone information. Also, you will need to default the time units.

1994-03-24 can be directly parsed into LocalDate as the modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        System.out.println(LocalDate.parse("1994-03-24"));
    }
}

Output:

1994-03-24

ONLINE DEMO

Demo of parsing with default time unit and a specific timezone:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .appendPattern("u-M-d[ H]")
                                .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)                                
                                .toFormatter(Locale.ENGLISH)
                                .withZone(ZoneId.systemDefault());
                                

        ZonedDateTime zdt = ZonedDateTime.parse("1994-03-24", dtf);
        System.out.println(zdt);
    }
}

Output:

1994-03-24T00:00Z[Europe/London]

ONLINE DEMO

Note: ZoneId.systemDefault() returns the JVM's ZoneId. Replace it with the applicable ZoneId e.g. ZoneId.of("America/New_York"). Also, notice the optional pattern inside the square bracket which has been defaulted to 0.

Learn more about the modern Date-Time API from Trail: Date Time.

Related