22

How can I get my date formatted as 2012-11-25T23:50:56.193+01:00 using SimpleDateFormat?
If I use Z in the format like

yyyy-MM-dd'T'hh:mm:ss.SSSZ

then it shows

2013-03-06T11:49:05.490+0100

Ivar
  • 6,138
  • 12
  • 49
  • 61
user2139437
  • 223
  • 1
  • 2
  • 6

2 Answers2

52

You can get the timezone offset formatted like +01:00 with the SimpleDateFormat in Java 7 (yyyy-MM-dd'T'HH:mm:ss.SSSXXX), or with the Joda's DateTimeFormat (yyyy-MM-dd'T'HH:mm:ss.SSSZZ).

ejain
  • 3,456
  • 2
  • 34
  • 52
  • 2
    is there a way for older javas without joda? – Karussell Dec 13 '13 at 08:04
  • 1
    See: http://stackoverflow.com/questions/13040143/generic-support-for-iso-8601-format-in-java-6 – Jerry Brady Dec 20 '13 at 17:05
  • 1
    I believe the format is with "HH" i.e. yyyy-MM-dd'T'HH:mm:ss.SSSXXX; as 'hh' will produce 00-12 and 'HH' 00-23. Also, from the other SO question linked. – Leo May 06 '16 at 03:22
  • Yes! This is answer and helped me figure out my problem of setting GMT like -05:00 and not -0500. – Tastybrownies Nov 16 '16 at 21:26
  • 1
    SimpleDateFormat that has been described in the answer is described on https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for reference. – James C Feb 06 '17 at 23:58
  • Note: Make sure you check this condition: it will only work for API 24+ https://developer.android.com/reference/kotlin/java/text/SimpleDateFormat – Rajesh Satvara Mar 02 '22 at 13:53
4

Here’s the 2017 answer. If there is any way you can (which there is), throw the outdated classes like SimpleDateFormat overboard and use the modern and more convenient classes in java.time. In particular, the desired format, 2012-11-25T23:50:56.193+01:00 complies with ISO-8601 and therefore comes out of the box with the newer classes, just use OffsetDateTime.toString():

    OffsetDateTime time = OffsetDateTime.now();
    System.out.println(time.toString());

This prints something like

2017-05-10T16:14:20.407+02:00

One thing you may or may not want to be aware of, though, it prints as many groups of 3 decimals on the seconds as it takes to print the precision in the OffsetDateTime object. Apparently on my computer “now” comes with a precision of milliseconds (seconds with three decimals).

If you have an oldfashioned Date object, for example, you got it from a call to some legacy method, I recommend the first thing you do is convert it to Instant, which is one of the modern classes. From there you can easily other conversions depending on your requirements:

    Date now = new Date();
    OffsetDateTime time = now.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();
    System.out.println(time.toString());

I am really doing more conversions than necessary. atZone(ZoneId.systemDefault()) produced a ZonedDateTime, and its toString() will not always give you the format you said you wanted; but it can easily be formatted into it:

    ZonedDateTime time = now.toInstant().atZone(ZoneId.systemDefault());
    System.out.println(time.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161