0

I am getting an parse error while parsing a date

java.text.ParseException: Unparseable date: "2021-06-17T05:49:41.174Z" Unparseable date: "2021-06-17T05:49:41.174Z"

my code looks like this

private static String generateAndValidate(int count) {
        Clock clock = Clock.systemUTC(); 
        String clockTime=clock.instant().toString();
        String result=clockTime;
        SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ",Locale.ENGLISH);
        try {
            output.parse(clockTime);
        } catch (ParseException e) {
            System.out.println("process date parse error. Going for retry.");
            
        }
        return result;
    }

Also tried hard coding the value here

SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ",Locale.ENGLISH);
        try {
            output.parse("2021-06-17T05:49:41.174Z");
        } catch (ParseException e) {
            System.out.println("process date parse error. Going for retry.");
            
        }

What could be the problem?

3
  • 2
    Since you can use java.time, the modern Java date and time API, don’t also mix in the old and troublesome SImpleDateFormat class. Clock is part of java.time. Also, what is the bigger goal you are trying to obtain? We can probably help you find a better way. Commented Jun 17, 2021 at 6:43
  • 3
    Why do you still use SimpleDateFormat? It has been obsolete since a long time. Use DateTimeFormatter instead. See deHaar's answer. Commented Jun 17, 2021 at 6:44
  • I have hesitatingly listed Parse Date String in Java [duplicate] as an original question. Beware that the accepted answer there is incorrect. The other answer, by achAmháin, is good. Commented Jun 17, 2021 at 6:48

2 Answers 2

4

EDIT: The reason for the failing of your code is in the answer given by @GS3!

My answer provides alternatives that are generally considered mroe up-to-date:

I would not recommend to use a java.text.SimpleDateFormat here because you are involving a very old and practically outdated API while you are receiving the time by the modern API utilizing a java.time.Clock.

A good move would be to use java.time.format.DateTimeFormatters for parsing, but I think you could even skip the clock and use OffsetDateTime.now(ZoneOffset.UTC).

However, this code definitely parses the String produced by your first lines:

public static void main(String[] args) {
    // your first two lines
    Clock clock = Clock.systemUTC();
    String clockTime = clock.instant().toString();
    // a hint to the problem
    System.out.println(clockTime + " <--- 6 fractions of second");
    // how to parse a String like that in plain java.time
    OffsetDateTime odt = OffsetDateTime.parse(clockTime);
    System.out.println(
        odt.format(
            DateTimeFormatter.ISO_OFFSET_DATE_TIME
        )
    );
}

The output of that will look like the following (obviously having different values):

2021-06-17T06:34:55.490370Z <--- 6 fractions of second
2021-06-17T06:34:55.49037Z

The output that uses a DateTimeFormatter.ISO_OFFSET_DATE_TIME is just one option, you can still define your own pattern using a DateTimeFormatter.ofPattern(yourPatternString), a DateTimeFormatterBuilder in order to handle optional parts or one of the other built-in formatters.

If you just want to get the current moment and store it in a some datetime class, you can use the now() method the datetime classes in java.time have:

OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);

looks suitable here, but there's a ZonedDateTime, too.

Just have a look at java.time...

Sign up to request clarification or add additional context in comments.

Comments

1

In SimpleDateFormat, Z represents a restricted subset of the RFC-822 time zone syntax. Instant::toString() provides a timestamp in the ISO-8601 format. You can fix this by using X instead of Z.

SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX",Locale.ENGLISH);

1 Comment

This definitely solves the particular problem here, so +1, but OP should stop using a SimpleDateFormat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.