How can I get the following format:
2015-01-31T00:00:00Z
(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! :)
How can I get the following format:
2015-01-31T00:00:00Z
(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! :)
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);
System.out.println(today);
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:
System.out.println(today.toString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")));
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);
@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.
DateTime dt = new DateTime();
DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
String str = fmt.print(dt);
Set up a DateTimeFormatter
:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
formatter.print(time);