2

I'm having trouble in formatting a string to a ZonedDateTime.

My customer wants to have the date in a format such as ddMMyyyyhhmmss, with no separators or stuff like that.

This is what I've done so far

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;


public class MyClass {
    public static void main(String args[]) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyyhhmmss");
        String test = formatter
        .format(ZonedDateTime.now()).toString();
        System.out.println(test);
        ZonedDateTime a = ZonedDateTime.parse(test, formatter);
        System.out.println(a.toString());
    }
}

While it correctly produces the string, the error occurs at the parsing process for creating the LocalDateTime variable:

28032019100707

Exception in thread "main" java.time.format.DateTimeParseException: Text '28032019100707' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 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 MyClass.main(MyClass.java:14)
Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 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: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed
    at java.time.ZoneId.from(ZoneId.java:466)
    at java.time.ZonedDateTime.from(ZonedDateTime.java:553)
    ... 4 more
Command exited with non-zero status 1

Searching on Stack Overflow, I saw that some answers to the same issue suggested to use the LocalDateTime class as an intermediate and then parse to ZonedDateTime, but it is still not working, throwing the same error.

I've also tried in changing the way I initialize a DateTimeFormatter with this procedure:

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyyhhmmss")
                          .toFormatter()
                          .withZone(ZoneId.systemDefault());

But it is still not working. How can I fix it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gianmarco F.
  • 780
  • 2
  • 12
  • 36
  • Related: [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. Mar 28 '19 at 10:18
  • 1
    In addition you need uppercase `HH` for hour of day in your format pattern string, as in [this question](https://stackoverflow.com/questions/17341214/difference-between-java-hhmm-and-hhmm-on-simpledateformat), for example. – Ole V.V. Mar 28 '19 at 10:20
  • @OleV.V. Do you think it is related even though it doesn't solve my issue? I'm just asking so that, next time, I can avoid to create a similar question – Gianmarco F. Mar 28 '19 at 10:20
  • I think the question linked under the duplicate marking matches yours better and should have a great chance of solving your issue too. See if it doesn’t and feel free to follow up here. – Ole V.V. Mar 28 '19 at 10:23
  • @OleV.V. I've tried that (if you see in my question, there is a DateTimeFormatBuilder that was partially taken from there), so that is why I opened a new question. But, as pointed out by YCF_L, all I needed was to directly use ZonedDateTime from the beginning – Gianmarco F. Mar 28 '19 at 10:25
  • 1
    That’s right: the string doesn’t contain enough information that `ZonedDateTime` can parse the same value back. – Ole V.V. Mar 28 '19 at 11:10
  • 2
    @OleV.V. thank you, though, for explaining why you marked it as duplicate :) – Gianmarco F. Mar 28 '19 at 11:10

3 Answers3

2

ddMMyyyyhhmmss does not contain any zone information. It's not ZonedDateTime, and it should be LocalDateTime.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • I already tried this approach but still have the same error – Gianmarco F. Mar 28 '19 at 10:13
  • @GianmarcoF. I also don't see why you would use ZonedDateTime. Using `String test = formatter.format(LocalDateTime.now()).toString(); LocalDateTime parsed = LocalDateTime.parse(test, formatter);` is doing just what you asked for. – CannedMoose Mar 28 '19 at 10:30
2

You want:

  String test = ZonedDateTime.now().format(formatter);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

In the string you parse, there isn't any information about the TimeZone, so how should the Formatter know how to transfer into ZonedDateTime?

You either need to have the TimeZone information in your string, or use another Object to parse it into which has no TimeZone infomation and afterwards transfer it into the Zone you need.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96
  • I've also tried to use LocalDateTime but I still get the same error message – Gianmarco F. Mar 28 '19 at 10:14
  • 2
    @GianmarcoF. no you did not get the same error message. The current error message explicitly states that it is missing information about the time zone. No matter what you say, that won't happen if you use LocalDateTime. If you have problems when using LocalDateTime, change your questions and show your exact code and exact results. – kumesana Mar 28 '19 at 10:21
  • Besides the problem with having no time zone information, it is correct that it also fails with a LocalDateTime - but this is because of using hh instead of HH in the DateTimeFormatter (as stated above). – Alexander Rühl Mar 28 '19 at 10:25
  • The error, in that case, was coming from the fact that hh was not with capital letters and it caused the error – Gianmarco F. Mar 28 '19 at 10:31