java JodaTime 删除秒数
java JodaTime remove seconds
我正在使用 JodaTime 创建 ISO 8601 字符串。
DateTime jodatime = new DateTime(2016, 04, 05, 23, 59, 59, 999, DateTimeZone.UTC);
String converted = jodatime.toDateTimeISO().toString();
现在,我得到以下信息:
2016-04-06T06:59:59.999Z
不过,我要truncate/remove秒和毫秒。
2016-04-05T23:59Z
有谁知道如何以最简单的方式做到这一点?
谁能告诉我日期解析库是否可以识别缩短版本的 ISO8601?
格式化 Joda 时间值的常规方法是使用格式化程序。在这种情况下,the format you want is already available,除了 Z:
DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinute();
String text = formatter.print(value);
Z
有点棘手 - 我不相信您可以使用简单的模式 (DateTimeFormat.forPattern
) 准确指定您想要的内容,但您可以使用 DateTimeFormatterBuilder
:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendYear(4, 9)
.appendLiteral('-')
.appendMonthOfYear(2)
.appendLiteral('-')
.appendDayOfMonth(2)
.appendLiteral('T')
.appendHourOfDay(2)
.appendLiteral(':')
.appendMinuteOfHour(2)
.appendTimeZoneOffset("Z", true, 2, 4)
.toFormatter()
.withLocale(Locale.US);
我相信这正是您想要的。
我正在使用 JodaTime 创建 ISO 8601 字符串。
DateTime jodatime = new DateTime(2016, 04, 05, 23, 59, 59, 999, DateTimeZone.UTC);
String converted = jodatime.toDateTimeISO().toString();
现在,我得到以下信息:
2016-04-06T06:59:59.999Z
不过,我要truncate/remove秒和毫秒。
2016-04-05T23:59Z
有谁知道如何以最简单的方式做到这一点? 谁能告诉我日期解析库是否可以识别缩短版本的 ISO8601?
格式化 Joda 时间值的常规方法是使用格式化程序。在这种情况下,the format you want is already available,除了 Z:
DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinute();
String text = formatter.print(value);
Z
有点棘手 - 我不相信您可以使用简单的模式 (DateTimeFormat.forPattern
) 准确指定您想要的内容,但您可以使用 DateTimeFormatterBuilder
:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendYear(4, 9)
.appendLiteral('-')
.appendMonthOfYear(2)
.appendLiteral('-')
.appendDayOfMonth(2)
.appendLiteral('T')
.appendHourOfDay(2)
.appendLiteral(':')
.appendMinuteOfHour(2)
.appendTimeZoneOffset("Z", true, 2, 4)
.toFormatter()
.withLocale(Locale.US);
我相信这正是您想要的。