JodaTime DateTime, ISO8601 GMT date format

对着背影说爱祢 提交于 2019-12-03 22:14:31

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);
okwap

@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.

use ISODateTimeFormat

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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!