Take the 2-minute tour ×

How can I get the following format:


(ISO8601 GMT date format)

Out of a DateTime object in joda time (java) ? Eg.

DateTime time = DateTime.now();
String str = // Get something like 2012-02-07T00:00:00Z

Thanks! :)

share|improve this question
What precisely do you want? Using the ISO8601 date format, you can output a date, for which the time zone is irrelevant. In your example, you include the time (which is not part of the date format) and in your example code, you only want "something like" that. –  jarnbjo Feb 7 '13 at 14:38
How is this not clear? He defined exactly what he wants. –  vikingsteve Dec 9 '13 at 12:09
add comment

4 Answers

up vote 6 down vote accepted

The JODA Javadoc indicates that toString for DateTime outputs the date in ISO8601. If you need to have all of the time fields zeroed out, do this:

final DateTime today = new DateTime().withTime(0, 0, 0, 0);

That will include milliseconds in the output string. To get rid of them you would need to use the formatter that @jgm suggests here.

If you want it to match the format you are asking for in this post (with the literal Z character) this would work:


If you need the value to be UTC, initialize it like this:

final DateTime today = new DateTime().withZone(DateTimeZone.UTC).withTime(0, 0, 0, 0);
share|improve this answer
Edit: this works perfect. –  petter Feb 7 '13 at 14:33
add comment

Set up a DateTimeFormatter:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
share|improve this answer
add comment

use ISODateTimeFormat

DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
String str = fmt.print(dt);
share|improve this answer
Note that ISODateTimeFormat.dateTime() ends in ZZ not Z. There's a difference (ZZ prepends a colon). –  jgm Feb 7 '13 at 14:10
Although it's absolutely not clear what exactly petter is asking about, your example differs from his example: It includes milliseconds in the output and uses the default time zone instead of UTC. –  jarnbjo Feb 7 '13 at 14:36
add comment

@jgm In the document, ISODateTimeFormat.dateTime() is described

Returns a formatter that combines a full date and time, separated by a 'T' (yyyy-MM-dd'T'HH:mm:ss.SSSZZ).

but if you set the timezone, e.g.,

DateTimeFormatter fmt = ISODateTimeFormat.dateTime();  
System.out.println(fmt.print(new DateTime().toDateTime(DateTimeZone.UTC)));

it will ends in Z.

share|improve this answer
Or also: fmt.print(DateTime.now(DateTimeZone.UTC)) –  vikingsteve Dec 9 '13 at 12:11
add comment

Your Answer


