-1

Possible Duplicate:
how to parse date in java?
How do I convert 2010-12-15T16:26:49.841-08:00 to a GregorianCalendar in Java?

Example string:

2009-06-04T11:17:14-07:00

I am not sure how to construct the formatter string for this one. Anyone can help please? Thanks! :)

Community
  • 1
  • 1
trillions
  • 3,669
  • 10
  • 40
  • 59

1 Answers1

4

This looks like a perfectly normal ISO-8601 date/time to me. You should be able to parse it with either SimpleDateFormat using the built-in APIs, or DateTimeFormatter in Joda Time, using the pattern "yyyy-MM-dd'T'HH:mm:ssX" (SimpleDateFormat) or "yyyy-MM-dd'T'HH:mm:ssZ" (DateTimeFormatter). Note that the "X" format symbol was only introduced into Java 7; if you need to do this in Java 6 or earlier you may need to strip the colon within the offset text (so -07:00 would become -0700).

Personally I'd advise you to use Joda Time anyway - it gets round the Java version issue, and it's much nicer API in general.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • `SimpleDateFormat` unfortunately [can't deal with the `:` in the timezone](http://stackoverflow.com/questions/4457108/how-do-i-convert-2010-12-15t162649-841-0800-to-a-gregoriancalendar-in-java/4459026#4459026). – Jesper Oct 08 '12 at 09:22
  • @Jesper: That answer was from December 2010, before Java 7 was released with the "Z" format specifier. – Jon Skeet Oct 08 '12 at 09:31
  • Ah, I didn't know that this was changed in Java 7! Thanks. – Jesper Oct 08 '12 at 09:46
  • I didn't know the change made in Java7 either. Thanks a lot for your advice! Currently i still use default Java 6. Can u help vote for my question? I think I asked a good question and learned a lot from your answer. Feel pretty upset on someone down vote my question...>. – trillions Oct 08 '12 at 11:15
  • @nanshi: Well, you didn't show what you'd tried - how far you'd got, etc... – Jon Skeet Oct 08 '12 at 11:30
  • how do i show how far i tried? I can't search on a string ended with "-07:00" and i dont know how to describe it in english. I know how to parse it without the end string. – trillions Oct 08 '12 at 11:37
  • why people are so picky while trying to help? if so, please don't help me! i only like nice people. – trillions Oct 08 '12 at 11:38
  • @nanshi: You're asking for help. People are giving their time willingly. I view it as only polite for the person *asking* the question to put time into asking a question as well as possible. Please read http://tinyurl.com/so-hints If you don't want me to help on future questions you ask, please say so - I'd rather not waste my time. – Jon Skeet Oct 08 '12 at 11:53
  • Thanks for your help, Jon! I was just upset about the down vote last night. – trillions Oct 08 '12 at 21:07