Java Timestamp string parsing
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开发者_C百科 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.
JodaTime may help. Consider using it and a custom formatter (called "freaky formatters").
http://joda-time.sourceforge.net/userguide.html#Input_and_Output
Unfortunately, the colon in the time zone complicates matters a bit. You might want to have a look at this question.
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
精彩评论