I have a date of this type: 2004-12-31 23:00:00-08 but no one of the patterns i know and i have used from the documentation is working. I thought it should something like "yyyy-MM-dd HH:mm:ssX" but it isn't working.
-
please write your code hereaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa– aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2014-11-06 11:53:24 +00:00Commented Nov 6, 2014 at 11:53
-
What should "-08" be? Time offest?Jens– Jens2014-11-06 11:57:52 +00:00Commented Nov 6, 2014 at 11:57
-
You should try using yyyy-MM-dd HH:mm:ssZZ" or "yyyy-MM-dd HH:mm:ssXX". There are two time zones RFC and ISO. Also, is there any reason why the timezone is not -0800, but -08?ha9u63a7– ha9u63a72014-11-06 11:58:08 +00:00Commented Nov 6, 2014 at 11:58
-
Can you let us know what are your expection in terms of timestamp i.e. how should it be post formatting?SMA– SMA2014-11-06 11:58:11 +00:00Commented Nov 6, 2014 at 11:58
-
2For me, the pattern "yyyy-MM-dd HH:mm:ssX" works fine. What problems do you have?Seelenvirtuose– Seelenvirtuose2014-11-06 12:06:24 +00:00Commented Nov 6, 2014 at 12:06
1 Answer
Sorry for you, but this is a known bug and was already reported in January 2014. According to the bug log a possible solution is deferred.
A simple workaround avoiding alternative external libraries is text preprocessing. That means: Before you parse the text you just append the prefix ":00". Example:
String input = "2004-12-31 23:00:00-08";
String zero = ":00";
if (input.charAt(input.length() - 3) == ':') {
zero = "";
}
ZonedDateTime zdt =
ZonedDateTime.parse(
input + zero,
DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssXXX"));
System.out.println(zdt);
// output: 2004-12-31T23:00-08:00
UPDATE due to debate with @Seelenvirtuose:
As long as you ONLY have offsets with just hours but without minute part then the pattern "uuuu-MM-dd HH:mm:ssX" will solve your problem, too (as @Seelenvirtuose has correctly stated in his comment).
But if you have to process a list of various strings with mixed offsets like "-08", "Z" or "+05:30" (latter is India standard time) then you should usually apply the pattern containing three XXX. But this currently fails (have verified it by testing in last version of Java-8). So in this case you still have to do text preprocessing and/or text analysis.