I am using SimpleDateFormat in order to parse a String. Here is an example:
private static final SimpleDateFormat longStringFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
[some code here]
Date dirDate = longStringFormat.parse("2014-04-03T06:00:00.376542900Z");
When I check dirDate in debug mode, or print it, the date I get is Mon Apr 07 14:35:42 FET 2014
.
Why do I get such an offset? It cannot be counted as a timezone offset(although it seems already wrong to apply an offset). How can I obtain a Date object depicting the exact same time as the String?
Well, the problem is that you're specifying this value for the number of milliseconds: 376542900. That's 104 hours, 35 minutes, 42 seconds and 900 milliseconds... hence the issue.
Unfortunately, it looks like SimpleDateFormat
doesn't have a way of understanding that you're giving it "fractions of a second" instead of "a number of milliseconds". I strongly suspect (although I haven't tried it) that Java 8 would work properly (possibly with a longer pattern) and that Joda Time may also handle it - are either of those an option? Otherwise, you'll need to use string manipulation to get the string into a more manageable form.
(As an aside, you should also set the SimpleDateFormat
's time zone to UTC, as that's what's being specified in the text.)
See more on this question at Stackoverflow