0

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

Sudarsana Kasireddy
  • 922
  • 1
  • 8
  • 24
  • Try setting the timezone: `fmt.setTimeZone( TimeZone.getTimeZone( "whatever timezone you need" ) );` – Thomas Apr 09 '15 at 16:29
  • Oh, I guess you mean the difference in the offset format, i.e. `-06:00` instead of `-04`. Please be a bit more specific next time so that we don't have to guess. – Thomas Apr 09 '15 at 16:40

2 Answers2

2

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();
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

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,':')));
Sudarsana Kasireddy
  • 922
  • 1
  • 8
  • 24