本文介绍了JodaTime DateTime,ISO8601 GMT日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得以下格式:

2015-01-31T00:00:00Z

(ISO8601 GMT日期格式)

(ISO8601 GMT date format)

Out of DateTime对象joda time(java)?例如。

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

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

谢谢! :)

推荐答案

JODA 表示 toString DateTime 输出ISO8601中的日期。如果您需要将所有时间字段清零,请执行以下操作:

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

这将包括输出字符串中的毫秒数。要摆脱它们,你需要使用@jgm建议的格式化程序。

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

如果你想让它与你在这篇文章中要求的格式相匹配(带有文字 Z 字符)这可行:

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'")));

如果您需要将值设为UTC,请按以下方式初始化:

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

这篇关于JodaTime DateTime,ISO8601 GMT日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:10