我想输出带有 PST 偏移量的时间戳(例如 2008-11-13T13:23:30-08:00)。
java.util.SimpleDateFormat
似乎没有以小时:分钟格式输出时区偏移量,它不包括冒号。有没有一种简单的方法可以在Java中获取时间戳?
// I want 2008-11-13T12:23:30-08:00
String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").format(new Date());
System.out.println(timestamp);
// prints "2008-11-13T12:23:30-0800" See the difference?
此外,
SimpleDateFormat
无法正确解析上面的示例。它抛出一个 ParseException
。
// Throws a ParseException
new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").parse("2008-11-13T13:23:30-08:00")
从 Java 7 开始,ISO8601 时区有
X
模式字符串。对于您描述的格式的字符串,请使用 XXX
。 请参阅文档。
样品:
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
.format(new Date()));
结果:
2014-03-31T14:11:29+02:00
查看Joda Time套餐。它们使 RFC 3339 日期格式变得更加容易。
乔达示例:
DateTime dt = new DateTime(2011,1,2,12,45,0,0, DateTimeZone.UTC);
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
String outRfc = fmt.print(dt);
我花了很多时间寻找同一问题的答案,我在这里找到了一些东西:http://developer.android.com/reference/java/text/SimpleDateFormat.html
建议答案:
String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZZZZZ").format(new Date());
如果您注意到我使用的是 5 个“Z”而不是 1 个。这使得输出的偏移量中带有冒号,如下所示:“2008-11-13T12:23:30-08:00”。希望有帮助。
“完成工作”部门的一种解决方案是在 SimpleDateFormat 完成后使用正则表达式来修复字符串。 Perl 中类似于 s/(\d{2})(\d{2})$/$1:$2/ 。
如果您对此感兴趣,我将使用有效的 Java 代码编辑此响应。
但是,是的。我也遇到这个问题。 RFC3339,我在看着你!
编辑:
这对我有用
// As a private class member
private SimpleDateFormat rfc3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String toRFC3339(Date d)
{
return rfc3339.format(d).replaceAll("(\\d\\d)(\\d\\d)$", "$1:$2");
}
问题在于 Z 生成的时区偏移量没有冒号 (:) 作为分隔符。
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ss.SZ");
这不是您真正需要的吗?
我们可以简单地使用 ZonedDateTime 类 和 DateTimeFormatter 类 来实现这一点。
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx");
ZonedDateTime z2 = ZonedDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.SECONDS);
System.out.println("format =======> " + z2.format(format));
输出:格式 =======> 2024-01-30T15:00:07-05:00
我尝试了这种格式并为我工作
yyyy-MM-dd'T'HH:mm:ss'Z'
我发现了一个流浪的 PasteBin 帮助我解决了这个问题:http://pastebin.com/y3TCAikc
以防万一其内容稍后被删除:
// I want 2008-11-13T12:23:30-08:00
String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").format(new Date());
System.out.println(timestamp);
// prints "2008-11-13T12:23:30-0800" See the difference?
// Throws a ParseException
new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").parse("2008-11-13T13:23:30-08:00")
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ss.SZ");
java.util
日期时间 API 及其格式化 API SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到现代日期时间 API*。
另外,下面引用来自Joda-Time主页的通知:
请注意,从 Java SE 8 开始,用户被要求迁移到 java.time (JSR-310) - JDK 的核心部分,它将取代该项目。
使用现代日期时间 API
java.time
的解决方案: 太平洋时区中最大的城市是洛杉矶,其时区名称为 America/Los_Angeles。使用 ZoneId.of("America/Los_Angeles")
,您可以创建 ZonedDateTime
的实例,该实例旨在在 DST 转换时自动调整时区偏移。
如果您需要时区偏移量而不是时区名称,您可以使用
ZonedDateTime
将 OffsetDateTime
转换为
ZonedDateTime#toOffsetDateTime
。 OffsetDateTime
的其他一些用途是创建具有固定时区偏移的日期时间实例(例如 Instant.now().atOffset(ZoneOffset.of("+05:30"))
,以及解析具有时区偏移的日期时间字符串。
演示:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
public class Main {
public static void main(String[] args) {
ZoneId zoneIdLosAngeles = ZoneId.of("America/Los_Angeles");
ZonedDateTime zdtNowLosAngeles = ZonedDateTime.now(zoneIdLosAngeles);
System.out.println(zdtNowLosAngeles);
// With zone offset but without time zone name
OffsetDateTime odtNowLosAngeles = zdtNowLosAngeles.toOffsetDateTime();
System.out.println(odtNowLosAngeles);
// Truncated up to seconds
odtNowLosAngeles = odtNowLosAngeles.truncatedTo(ChronoUnit.SECONDS);
System.out.println(odtNowLosAngeles);
// ################ A winter date-time ################
ZonedDateTime zdtLosAngelesWinter = ZonedDateTime
.of(LocalDateTime.of(LocalDate.of(2021, 11, 20), LocalTime.of(10, 20)), zoneIdLosAngeles);
System.out.println(zdtLosAngelesWinter); // 2021-11-20T10:20-08:00[America/Los_Angeles]
System.out.println(zdtLosAngelesWinter.toOffsetDateTime()); // 2021-11-20T10:20-08:00
// ################ Parsing a date-time string with zone offset ################
String strDateTime = "2008-11-13T13:23:30-08:00";
OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
System.out.println(odt); // 2008-11-13T13:23:30-08:00
}
}
示例运行的输出:
2021-07-18T03:27:15.578028-07:00[America/Los_Angeles]
2021-07-18T03:27:15.578028-07:00
2021-07-18T03:27:15-07:00
2021-11-20T10:20-08:00[America/Los_Angeles]
2021-11-20T10:20-08:00
2008-11-13T13:23:30-08:00
您一定已经注意到,我没有使用
DateTimeFormatter
来解析您问题的日期时间字符串。这是因为您的日期时间字符串符合 ISO-8601 标准。现代日期时间 API 基于 ISO 8601,只要日期时间字符串符合 ISO 8601 标准,就不需要显式使用 DateTimeFormatter
对象。
从 Trail:日期时间了解有关现代日期时间 API 的更多信息。
* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用 ThreeTen-Backport,它将大部分 java.time 功能向后移植到 Java 6 和 7。正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖可用的 Java 8+ API 和 如何在 Android 项目中使用 ThreeTenABP。
我用这个测试了很多,对我来说效果很好......特别是在解析(以及格式化)方面,它是迄今为止我发现的最接近的
DateTimeFormatter rfc3339Formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter rfc3339Parser = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendValue(ChronoField.YEAR, 4)
.appendLiteral('-')
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('T')
.appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 2, 9, true) //2nd parameter: 2 for JRE (8, 11 LTS), 1 for JRE (17 LTS)
.optionalEnd()
.appendOffset("+HH:MM","Z")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT)
.withChronology(IsoChronology.INSTANCE);
测试用例位于 https://github.com/guyplusplus/RFC3339-DateTimeFormatter
使用谷歌图书馆
new com.google.api.client.util.DateTime(new Date()).toStringRfc3339()