1

I need to convert a string time stamp value into Java Date object. The string is in '2011-03-16T09:00:00-05:00' format. Is there a time zone representation i can use to load this data as a Date object using SimpleDateFormat? 'z','Z' and 'zzzz' are the only time zone representations i am aware of and none of those can represent my time zone data (-05:00). Has anyone solved this problem?

Thanks.

4
  • 1
    You should be able to match "00:00-05:00" with a single z (lowercase). This doesn't work? Commented Mar 22, 2011 at 20:15
  • I think the : symbol throws it off: SimpleDateFormat wants "-0500", not "-05:00". One way to solve this - bite the bullet, manually remove that colon, then pass it to SimpleDateFormat Commented Mar 22, 2011 at 20:21
  • Could you post some code? There are a couple different ways you could be approaching this, and how we answer depends on which one you're using. Commented Mar 22, 2011 at 20:23
  • @iluxa Not true, see: download.oracle.com/javase/1.4.2/docs/api/java/text/…. It has the example string "GMT-08:00" that is supposed to match against z Commented Mar 22, 2011 at 20:23

3 Answers 3

1

JodaTime may help. Consider using it and a custom formatter (called "freaky formatters").

http://joda-time.sourceforge.net/userguide.html#Input_and_Output

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

2 Comments

Thanks. This is a nice solution. For now, because i need it only for one odd case, i am going to consider some string regex as suggested by eaj and use the same old SimpleDateFormat. Thanks anyways.
No problem. You'll come around to Joda Time eventually. Took me a few projects to buy in to it. :D
0

Unfortunately, the colon in the time zone complicates matters a bit. You might want to have a look at this question.

Comments

0

Seeing that this timestamp format provided looks like the standard format used in XML, you could try the following:

public static void main(String[] args) throws DatatypeConfigurationException {

    String inDate = "2011-03-16T09:00:00-05:00";

    javax.xml.datatype.DatatypeFactory factory = DatatypeFactory.newInstance();

    javax.xml.datatype.XMLGregorianCalendar xmlGregCal = factory.newXMLGregorianCalendar(inDate);

    java.util.GregorianCalendar gregCal = xmlGregCal.toGregorianCalendar();

    java.util.Date dateObj = gregCal.getTime();

    System.out.println("cal = " + xmlGregCal.toString());
    System.out.println("cal.year = " + xmlGregCal.getYear());
    System.out.println("cal.month = " + xmlGregCal.getMonth());
    System.out.println("cal.day = " + xmlGregCal.getDay());
    System.out.println("cal.hour = " + xmlGregCal.getHour());
    System.out.println("cal.minute = " + xmlGregCal.getMinute());
    System.out.println("cal.second = " + xmlGregCal.getSecond());
    System.out.println("cal.timezone = " + xmlGregCal.getTimezone());
    System.out.println("cal.eonAndYear = " + xmlGregCal.getEonAndYear());
}

The output created is as follows:

cal = 2011-03-16T09:00:00-05:00
cal.year = 2011 cal.month = 3
cal.day = 16
cal.hour = 9
cal.minute = 0
cal.second = 0
cal.timezone = -300
cal.eonAndYear = 2011

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.