-2

I am trying to parse String into a Date object using SimpleDateFormat. Below is my code

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm.sss");
Date parsedDate = formatter.parse("2016-06-28T14:10:23.374Z");
System.out.println(parsedDate.toString());

This is giving me below exception

Exception in thread "main" java.text.ParseException: Unparseable date: "2016-06-28T14:10:23.374Z"
at java.text.DateFormat.parse(DateFormat.java:368)
at com.bac.rds.dolphin.test.Main.main(Main.java:60)

My understanding of the problem is that the Format String I am using in SDF may not be appropriate.I am not much familiar with SimpleDateFormat any help to get this working is appreciated.

EDIT

Have updated my format String as below

yyyy-MM-dd'T'HH:mm:ss.SSS

This format string is able to parse the String into date object . But when I try to include Z at the end of the format string inorder to specify the timezone as the Z is there also in the Date String I have . It gives parse exception .

Below is the format throwing parse exception

yyyy-MM-dd'T'HH:mm:ss.SSSZ

Why this behavior?

Zulfi
  • 586
  • 4
  • 14
  • 5
    Look at your pattern - it doesn't end with Z, but the value does. Likewise your pattern uses `hh` which is a 1-12 hour-of-day format specifier, but your value has the value of 14 for that. Did you read the `SimpleDateFormat` documentation to check every part of your pattern? – Jon Skeet Jun 29 '16 at 06:54
  • have a look at this doc to understand the parsing methods: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Riad Jun 29 '16 at 06:57
  • 2
    Also, you are trying to parse the milliseconds as seconds and have no pattern for parsing the seconds. – DanielBarbarian Jun 29 '16 at 07:01
  • @JonSkeet .. changed the format string after going through the docs .. still getting error .. please see my edit on the question – Zulfi Jun 29 '16 at 07:18
  • See Javadoc: "unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string". You need to quote the 'Z', otherwise it's interpreted as a 4-digit RFC822 time zone – Erwin Bolwidt Jun 29 '16 at 07:20
  • 2
    Look at what the `Z` format specifier means. You should either use `X` instead, or quote the `Z` like you've quoted the `T`, and set the time zone to UTC in the formatter. – Jon Skeet Jun 29 '16 at 07:20
  • Thanks a lot ..guys – Zulfi Jun 29 '16 at 07:46
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 03 '18 at 06:20
  • Modern approach is simply: `java.time.Instant.parse( "2016-06-28T14:10:23.374Z" )` – Basil Bourque Sep 03 '18 at 06:49

1 Answers1

0

Try this

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Date parsedDate = formatter.parse("2016-06-28T14:10:23.374Z");
System.out.println(parsedDate.toString());

and output is

Tue Jun 28 14:10:23 IST 2016
Bhargav Kumar R
  • 2,190
  • 3
  • 22
  • 38
  • 1
    So you're parsing 10 for minutes and 23 for seconds, but the output is 16 for minutes and 14 for seconds. That doesn't sound right, does it? – Erwin Bolwidt Jun 29 '16 at 07:15
  • @ErwinBolwidt Do you know why? – Bhargav Kumar R Jun 29 '16 at 07:19
  • Lowercase s will parse seconds, which you have put in twice in your pattern. Uppercase S parses milliseconds. – DanielBarbarian Jun 29 '16 at 07:25
  • Still wrong. If you escape the letter Z in your pattern then you must set the timezone to UTC (GMT0) on your `SimpleDateFormat`-object in order to handle the letter Z of the input in a correct way (the way to go for older Java-versions before 7). Or better use the pattern symbol X (available in Java-7). – Meno Hochschild Jun 29 '16 at 13:11
  • FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 03 '18 at 06:20