JodaTime:如何从 DateTime 对象获取 ISO 格式的时间?

JodaTime : How to get ISO formatted time from DateTime object?

我正在使用 Joda 获取 DTO:

dateTimeObj = new DateTime();

我想将此 DTO 转换为 ISO 格式的格式化字符串:

2016-03-06T11:30:00-05:00

我尝试使用:

dateTimeObj = new DateTime().toDateTimeISO();

但没有运气。

请问我怎样才能以正确的方式做到这一点?

toDateTimeISO() 已弃用,请使用 toDateTime() http://joda-time.sourceforge.net/apidocs/org/joda/time/Instant.html

还有:

DateTime dt = new DateTime();
DateTimeFormatter fmt = DateTimeFormat.forPattern("MMMM, yyyy");
String str = fmt.print(dt);

请在此处查看模式语法: http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html

首先,您需要使用 LocalDateTime 而不是 DateTime 才能访问您格式的时区。

使用 DateTimeFormatter with the format defined as in the chart here.

您想要的格式将是 "YYYY-MM-DDTHH:mm:ss-ZZZZ"。

您可以使用 IsoDateTimeFormat.dateTimeNoMillis() 获取没有毫秒部分的格式:

 DateTime dt = new DateTime();
 DateTimeFormatter fmt = ISODateTimeFormat.dateTimeNoMillis();
 String str = fmt.print(dt);

您可以像这样调用 format 方法:

.format(DateTimeFormatter.ISO_INSTANT));