问题
I need to convert date to this format 2015-02-27T15:14:13-06:00
I have tried with SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
, but this one generating as 2015-04-09T10:39:19-04
Tks
回答1:
You can either use the new Yoda time which has ISO date formatters and parsers or you can use a SimpleDateFormat
of "yyyy-MM-dd'T'HH:mm:ssZ" which gives you "ccyy-mm-ddThh:mm:ss+zzzz" and insert the extra colon manually.
Something like:
new StringBuilder(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(date)).insert(22,':').toString();
回答2:
thanks oldcurmudgeon. I needed to set timezone and add your logic to insert : at 22nd position
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
fmt.setTimeZone( TimeZone.getTimeZone("US/Central"));
System.out.println("date to str:"+(new StringBuilder(fmt.format(new Date())).insert(22,':')));
来源:https://stackoverflow.com/questions/29543947/how-to-convert-date-to-this-format-2015-02-27t151413-0600