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.

pjames
  • 13
  • 1
  • 3
  • 1
    You should be able to match "00:00-05:00" with a single `z` (lowercase). This doesn't work? – Travis Webb Mar 22 '11 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 – iluxa Mar 22 '11 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. – Pops Mar 22 '11 at 20:23
  • @iluxa Not true, see: http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html. It has the example string "GMT-08:00" that is supposed to match against `z` – Travis Webb Mar 22 '11 at 20:23

3 Answers3

1

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

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

Freiheit
  • 8,408
  • 6
  • 59
  • 101
  • 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. – pjames Mar 22 '11 at 21:10
  • No problem. You'll come around to Joda Time eventually. Took me a few projects to buy in to it. :D – Freiheit Mar 24 '11 at 16:22
0

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

Community
  • 1
  • 1
eaj
  • 2,576
  • 1
  • 20
  • 42
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

crowne
  • 8,456
  • 3
  • 35
  • 50