0

I am struggling with a date string, I need to parse into the java ‘Date’ object. Here is what I have got so far:

try {

String value =   "2017‎-‎11‎-‎23T14:00:49.184000000Z";
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'";

SimpleDateFormat parser = new SimpleDateFormat(pattern);

Date date = parser.parse(value);


} catch (ParseException e) {e.printStackTrace();}

It currently throws a ParseExceptionUnparseable date” and I can’t get it to work.

Any help is highly appreciated!

Thanks

Jonas_Hess
  • 1,874
  • 1
  • 22
  • 32
  • 3
    There are only `999` milliseconds, so your desire to add nanoseconds isn't going to work. You should look at the new `java.time` package, and prefer [`DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html). – Elliott Frisch Dec 21 '17 at 13:57
  • Yes Elliot is right, 184000 ms (in the int range) might work, but would add seconds, minutes, hours to the date. – Joop Eggen Dec 21 '17 at 14:13
  • 3
    In your value there is an invisible left-to-right mark (Unicode 8206 or hex 200E) between the year and the first hyphen. This prevents parsing. There are four of them in the string in total, one before and one after each hyphen. Luckily you got them pasted into your question so we can detect them! – Ole V.V. Dec 21 '17 at 14:39
  • I am sorry, but the answer to the linked question will not be able to deal with the control characters in your string either. – Ole V.V. Dec 21 '17 at 14:55
  • Link: [Unicode Character 'LEFT-TO-RIGHT MARK' (U+200E)](http://www.fileformat.info/info/unicode/char/200e/index.htm). – Ole V.V. Dec 21 '17 at 14:56
  • @JoopEggen, a detail in the big picture, 184 000 000 (nine digits) is still comfortably within the `int` range. The result of parsing this with `SSSSSSSSS` in `SimpleDateFormat` is an incorrect date-time, not an exception. – Ole V.V. Dec 21 '17 at 15:10
  • Thanks @OleV.V.; did somewhere see an overflow of ms. – Joop Eggen Dec 21 '17 at 15:17
  • FYI, the string format seen here is defined by the ISO 8601 standard. The standard formats are used by default in the java.time classes when parsing/generating strings. – Basil Bourque Dec 21 '17 at 15:53
  • 1
    @BasilBourque, it certainly *looks* that way. However, the ISO 8601 standard does not include the invisible control characters found in the string in the question that caused the exception. The OP needs to get rid of those, and will then benefit from using the modern classes. – Ole V.V. Dec 21 '17 at 15:59

2 Answers2

7

Use Instant from java.time package (java 8) instead, it should look like below

String value = "2017-11-23T14:00:49.184000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
System.out.println(date);
johnII
  • 1,423
  • 1
  • 14
  • 20
  • 2
    Thanks for this solution, this works as expected. But I still had some trouble to get it to work. As @Ole V.V. has pointed out, my incoming value string had some invisible uft-8 characters in them that confused the parsing. – Jonas_Hess Dec 21 '17 at 15:27
0

you can use timeZone as well like this as another solution.

TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
            Calendar cal = Calendar.getInstance(tz);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");
            sdf.setCalendar(cal);
            cal.setTime(sdf.parse("2017-11-23T14:58:00.184000000Z"));
            Date date = cal.getTime();
            System.out.println(date);
Mahesh
  • 78
  • 1
  • 11
  • Won’t work either, sorry. – Ole V.V. Dec 21 '17 at 14:23
  • I have Tried this one working fine for me. – Mahesh Dec 21 '17 at 14:37
  • Expected result (since I am at UTC+1 at this time of year): Thu Nov 23 15:58:00 CET 2017. Observed result: Sat Nov 25 13:34:40 CET 2017. Two days off. Which result do you get? – Ole V.V. Dec 21 '17 at 14:44
  • i think problem with timezone. use your time zone instead "Asia/Calcutta" – Mahesh Dec 21 '17 at 14:49
  • It’s not a time zone issue. Your time zone and mine are 4 hours 30 minutes apart, that cannot account for a difference of 2 days. Your answer does not address the problem of control characters in the input in the question, and it repeats the question’s error of trying to parse nanoseconds with `SSSSSSSSS`, which in `SimpleDateFormat` means milliseconds, an error of a factor one million. – Ole V.V. Dec 21 '17 at 14:59
  • 1
    The troublesome classes used here are now legacy, entirely supplanted by the industry-leading java.time classes. – Basil Bourque Dec 21 '17 at 15:58