how to convert date to this format 2015-02-27T15:14:13-06:00

孤街醉人 提交于 2020-01-07 00:33:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!