Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java date offset format issue?

Tags:

java

My date format is yyyy-MM-dd'T'HH:mm:ss.SSSZ, which is producing the date :

2013-10-08T12:14:39.721+0100

But I need the date to be :

2013-10-08T12:14:39.721+01:00

what date format will generate the offset with a colon?

like image 992
user86834 Avatar asked Oct 23 '25 18:10

user86834


2 Answers

You can use this format

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

Have a look at the doc for more info.

P.S:- As my friend @Thomas mentioned, this will work only with Java 7 and above.

like image 161
Rahul Avatar answered Oct 26 '25 07:10

Rahul


If you want to implement using SimpleDateFormat you can use R.J. solution which will work for JDK 7. You can also implement the same using Joda time as below

DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
String str = fmt.print(dt);
System.out.println(str);

which outputs,

2013-10-08T20:36:19.802+09:00
like image 35
Jayamohan Avatar answered Oct 26 '25 07:10

Jayamohan



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!