Parsing timestamp with time zone in Java?

I'm trying to parse a string of time stamps using the time zone obtained from DB The string is as follows:

SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSZ");

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
    Date d1 = mdyFormat.parse("2014-04-01 15:19:49.31146+05:30");

    String mdx = sdf.format(d1);

    System.out.println(mdx);

The problem is, I got an error saying:

Exception in thread "main" java.text.ParseException: Unparseable date: "2014-04-01 15:19:49.31146+05:30"
    at java.text.DateFormat.parse(DateFormat.java:357)
    at com.karthik.Timestampvalidate.main(Timestampvalidate.java:31)

Who knows how to solve this problem?

Solution

You need to use x instead of Z:

SimpleDateFormat mdyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSX");

For details, see the Javadoc

Note: Java 7.0 only

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>