6

Currently we are using

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX")

To format the time range to an external vendor for a range of data, for India, that formatter will give time like this:

2018-04-26T00:00:00.000+0530

However, my vendor say they cannot accept this format and it have to look like

2018-04-26T00:00:00.000+05:30

However, look like in DateTimeFormatter, whatever I choose Z/z/X/x, I don't get that format of offset. Just wonder is that a way to customize the offset to be HH:mm?

Or, I need to get the offset in second and work that our myself?

carfield
  • 2,011
  • 2
  • 15
  • 15

1 Answers1

6

It is three x. Just tried with JavaRepl:

java.time.format.DateTimeFormatter
    .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx")
    .withZone(java.time.ZoneId.systemDefault())
    .format(java.time.Instant.now())

Results in

java.lang.String res10 = "2018-04-27T11:06:50.648+00:00"

After some trial and error, I saw that this is also documented in the API documentation of DateTimeFormatter but it is not easy to find (buried in a lot of other text):

Three letters outputs the hour and minute, with a colon, such as '+01:30'

DateTimeFormatter API Documentation

David Tanzer
  • 2,732
  • 18
  • 30