2

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?

user1068636
  • 1,871
  • 7
  • 33
  • 57
  • 1
    For me finding the linked original question was not hard. Wondering how hard you searched before asking? – Ole V.V. Jul 19 '21 at 07:46
  • 1
    Does this answer your question? [Unable to obtain ZonedDateTime from TemporalAccessor using DateTimeFormatter and ZonedDateTime in Java 8](https://stackoverflow.com/questions/23596530/unable-to-obtain-zoneddatetime-from-temporalaccessor-using-datetimeformatter-and) – Ole V.V. Jul 23 '21 at 03:20

1 Answers1

5

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • You need to move `.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)` to *after* the pattern. Otherwise, a string with an hour component cannot be parsed. – MC Emperor Jul 22 '21 at 11:26
  • @MCEmperor - Thanks for the catch. As far as I can recall this is the second time on SO I have done this mistake in a hurry. I've updated the answer now. – Arvind Kumar Avinash Jul 22 '21 at 11:39
  • Well, I noticed because I recently [answered](https://stackoverflow.com/questions/68472713/java-time-format-datetimeparseexception-text-could-not-be-parsed-at-index/68472816#68472816) a question with the same problem. – MC Emperor Jul 22 '21 at 12:02