-1

I am trying

new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ssZ").format(Calendar.getInstance().getTime()) 

and the output which I am getting is 2018-16-11T07:16:48+0530. But the expected one is 2018-16-11T07:16:48+05:30.

Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 1
    Your date format string is wrong; use `MM` for months, not `mm`. – Jesper Sep 11 '18 at 14:07
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Apart from that your problem is the opposite of this one: [Not able to understand “YYYY-MM-DDTHH:MM:SS” date format](https://stackoverflow.com/questions/50271274/not-able-to-understand-yyyy-mm-ddthhmmss-date-format). – Ole V.V. Sep 12 '18 at 07:08
  • You expect `2018-16-11T07:16:48+05:30`?? What was that supposed to mean? The 11th day of the 16th month?? It looks like what you really want is ISO 8601 format. I suggest you can live with `OffsetDateTime.now(ZoneId.of("Asia/Kolkata")).toString()` (substitute your time zone of choice). It gives a string like `2018-09-12T12:50:11.265+05:30`, that is, with milliseconds, even microseconds depending on what the platform supports. For most purposes this won’t pose a problem. – Ole V.V. Sep 12 '18 at 07:21

2 Answers2

1

You have to use XXX instead of Z for timezone to get it in the format you want:

new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssXXX");

Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

amicoderozer
  • 2,046
  • 6
  • 28
  • 44
1

You should use like this..

new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
arjunan
  • 217
  • 1
  • 9