0

I need to format a ZonedDate time to format MM/dd/yyyy.

ZonedDateTime zonedDateTime = ZonedDateTime.now();
String date = DateTimeFormatter.ofPattern("MM/dd/yyyy").format(zonedDateTime);
ZonedDateTime zoneDate = ZonedDateTime.parse(date);

Getting error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '12/05/2018' could not be parsed at index 0

Or if I convert my value to a String with the format I want and then try to parse it back a ZonedDate Time with my format once again:

DateTimeFormatter format = DateTimeFormatter.ofPattern("MM/dd/yyyy");
ZonedDateTime zonedDateTime = ZonedDateTime.now();
String date = DateTimeFormatter.ofPattern("MM/dd/yyyy").format(zonedDateTime);
ZonedDateTime zonedate = ZonedDateTime.parse(date, format);

I get error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '12/05/2018' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2018-12-05 of type java.time.format.Parsed

I've seen plenty of questions on this, but I keep getting these parsing errors

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Drew13
  • 1,301
  • 5
  • 28
  • 50
  • That's not the format [`parse`](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#parse-java.lang.CharSequence-) expects. Use the other override of it that [takes a `DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#parse-java.lang.CharSequence-java.time.format.DateTimeFormatter-) created with your format of "MM/dd/yyyy" and it should parse. – David Conrad Dec 05 '18 at 20:25
  • 3
    If all you have is a month, day, and year, you should be using [LocalDate](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/LocalDate.html), not ZonedDateTime. – VGR Dec 05 '18 at 20:27
  • @DavidConrad I edited the question to include that I have already tried that along with the error that I receive. – Drew13 Dec 05 '18 at 20:28
  • Yeah, sorry, I saw your edit just as a posted my comment. – David Conrad Dec 05 '18 at 20:31
  • @DavidConrad for some reason it still can't parse even though the String is already formatted the way I want it to be and I pass the same format to the `parse` method. – Drew13 Dec 05 '18 at 20:42
  • @OleVV This isn't really a duplicate of that since in that case the OP was specifying the zone and was only missing the time; in this case, both time and zone were missing. But it is very similar. – David Conrad Dec 07 '18 at 18:15

1 Answers1

2

There are two problems. First, there is no zone information in the date, and second, there is no time information. You can convert it to a LocalDate:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
ZonedDateTime zonedDateTime = ZonedDateTime.now();
String date = formatter.format(zonedDateTime);
LocalDate localdate = LocalDate.parse(date, formatter);

And you can convert a LocalDate to a ZonedDateTime by setting the time to the time at start of day, and the zone to the default system zone. Otherwise, you'd need to provide a time and a ZoneId of your choosing.

ZonedDateTime zdt = localdate.atStartOfDay(ZoneId.systemDefault());
David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • I essentially found that error here https://stackoverflow.com/questions/23596530/unable-to-obtain-zoneddatetime-from-temporalaccessor-using-datetimeformatter-and . But `zdt` is still in the default ZonedDateTime format. `localdate` is actually in the format I want, but still need a ZonedDateTime object. – Drew13 Dec 05 '18 at 20:47
  • 2
    At this point it might just be easiest to use `LocalDate` like @VGR suggests – Drew13 Dec 05 '18 at 20:55
  • 1
    I don't understand. A `ZonedDateTime` isn't in any format. That's why you have to use a `DateTimeFormatter` to format it. – David Conrad Dec 05 '18 at 21:05
  • Maybe you mean that when you convert it to a String by calling `toString()` on it, it comes out in ISO format. But that doesn't mean it's in that format. It gets converted to that format by calling `toString()` or converted to whatever other format you want by a `DateTimeFormatter`. – David Conrad Dec 05 '18 at 21:06
  • Correction: `ZonedDateTime.toString()` does not define a valid ISO-format because of the trailing zone id which is taken from IANA-TZDB, not from ISO (which only knows offsets). – Meno Hochschild Dec 07 '18 at 17:19
  • @MenoHochschild It can be. As the docs say, "The output is compatible with ISO-8601 if the offset and ID are the same." `ZonedDateTime zdt = ZonedDateTime.now(); System.out.println(zdt.withZoneSameInstant(zdt.getOffset()));` – David Conrad Dec 07 '18 at 17:36
  • That is just a very special edge case to use offsets as ids. In general, zone ids like "America/New_York" are not compatible with ISO-8601. – Meno Hochschild Dec 07 '18 at 17:39