如何将日期格式从
"2021-02-28 13:38:00.597+0000"
转换为"Mon, Feb 28,2021 15:25:00 UTC"
UTC格式的Scala代码?"2021-02-28 13:38:00.597+0000"
转换为"Mon, Feb 28,2021 15:25:00 UTC"
UTC格式的Scala代码?+0000
时区偏移量是UTC,因此我可以省略withZoneUTC()
,但我仍然在第一个日期中使用它以确保安全。 val oldDateString = "2021-02-28 13:38:00.597+0000"
val OldFormat = "yyyy-MM-dd HH:mm:ss.SSSZ"
val NewFormat = "EEE, MMM dd, yyyy HH:mm:ss z"
val formatterOld = DateTimeFormat.forPattern(OldFormat)
val formatterNew = DateTimeFormat.forPattern(NewFormat)
val dt = formatterOld.withZoneUTC().parseDateTime(oldDateString)
val dateStringInUTC = formatterNew.withZoneUTC().print(dt)
println(dt) // 2021-02-28T13:38:00.597Z
println(dateStringInUTC) // Sun, Feb 28, 2021 13:38:00 UTC
更新: 对于Java 8及更高版本,java.time API是您的好朋友。同样,withZoneSameInstant(ZoneOffset.UTC)
并不是真正需要的: val oldDateString = "2021-02-28 13:38:00.597+0000"
val OldFormat = "yyyy-MM-dd HH:mm:ss.SSSZZZ"
val NewFormat = "EEE, MMM dd, yyyy HH:mm:ss z"
val formatterOld = DateTimeFormatter.ofPattern(OldFormat)
val formatterNew = DateTimeFormatter.ofPattern(NewFormat)
val zdt = ZonedDateTime.parse(oldDateString, formatterOld)
val dateStringInUTC = zdt.withZoneSameInstant(ZoneId.of("UTC")).format(formatterNew)
println(zdt) // 2021-02-28T13:38:00.597Z
println(dateStringInUTC) // Sun, Feb 28, 2021 13:38:00 UTC
更新:由于后者无法在结尾处打印出字符串“UTC”,即使ZoneOffset
扩展了ZoneId
,因此已切换到使用ZoneId.of("UTC")
而不是ZoneOffset.UTC
。
import
语句。所有三个项目(Joda-Time、java.time 和 ThreeTen-Backport)均由同一人 Stephen Colebourne 领导。 - Basil Bourquejava.time
,则需要:
DateTimeFormatter
来解析具有您输入示例格式的String
,这种格式非常接近ISO标准,但缺少日期和时间之间的'T'
DateTimeFormatter
将时间内容以所需格式输出,其中包括星期几和月份的英文缩写OffsetDateTime
用于使用第一个DateTimeFormatter
解析String
ZonedDateTime
表示UTC的时间值public static void main(String[] args) {
// example String
String utcDatetimeString = "2021-02-28 13:38:00.597+0000";
// prepare a formatter that can parse a String of this format
DateTimeFormatter dtfIn = DateTimeFormatter.ofPattern(
"uuuu-MM-dd HH:mm:ss.SSSxxxx",
Locale.ENGLISH
);
// parse it to an OffsetDateTime
OffsetDateTime odt = OffsetDateTime.parse(utcDatetimeString, dtfIn);
// then convert it to a ZonedDateTime applying UTC zone
ZonedDateTime zdt = odt.atZoneSameInstant(ZoneId.of("UTC"));
// prepare a formatter that produces the desired output
DateTimeFormatter dtfOut = DateTimeFormatter.ofPattern(
"EEE, MMM dd, uuuu HH:mm:ss zzz",
Locale.ENGLISH
);
// and print the ZonedDateTime using the formatter
System.out.println(zdt.format(dtfOut));
}
输出:
Sun, Feb 28, 2021 13:38:00 UTC
ZoneId.of("UTC")
非常重要。我之前使用的是ZoneOffset.UTC
,导致使用OffsetDateTime
时抛出DateTimeException: Unable to extract ZoneId from temporal ...
异常,因此我改用了ZonedDateTime
,但最终无法打印出UTC
的字符串,所以不得不手动插入它。 - Alin Gabriel ArhipZoneId
来代替ZonedDateTime
,即使ZoneOffset
扩展了ZoneId
。我认为我们两个的答案都有价值!是的,必须有一个带有ID的区域,仅有偏移量是不够的(尽管+0000在所有情况下都表示UTC)。 - deHaar
java.time
来实现,但是我们无法更改星期几(事实上,2021年2月28日是星期天)。如果只是格式问题,请展示您的尝试(当然不一定要成功),我会尽力给出答案。 - deHaarSimpleDateFormat
。它是一个出了名的麻烦类,而且现在已经过时了。正如@deHaar所说,你应该使用java.time。 - Ole V.V.DateTimeFormatter.RFC_1123_DATE_TIME
似乎只能提供所需的内容,但存在上述差异。 - Ole V.V.