1

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.

11
  • please write your code here Commented Nov 6, 2014 at 11:53
  • What should "-08" be? Time offest? Commented 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? Commented 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? Commented Nov 6, 2014 at 11:58
  • 2
    For me, the pattern "yyyy-MM-dd HH:mm:ssX" works fine. What problems do you have? Commented Nov 6, 2014 at 12:06

1 Answer 1

2

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.