Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Date in java - Time zone

Tags:

java

date

I have a String, 2013-10-07T23:59:51.205-07:00, want to convert this to Java date object. I am getting parsing error.

date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse("2013-10-07T23:59:51.205-07:00");
like image 574
Amit Saxena Avatar asked Jan 16 '26 21:01

Amit Saxena


1 Answers

try

date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
                 .parse("2013-10-07T23:59:51.205-0700");

The Z is not a literal and the timezone does not have a colon

See the examples at http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

If java7 is being used then Z can be replaced with X and the timezone can have a colon

like image 56
Scary Wombat Avatar answered Jan 19 '26 18:01

Scary Wombat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!