在 Java 中输出 RFC 3339 时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/289311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output RFC 3339 Timestamp in Java
提问by Cristian
I want to output a timestamp with a PST offset (e.g., 2008-11-13T13:23:30-08:00). java.util.SimpleDateFormat
does not seem to output timezone offsets in the hour:minuteformat, it excludes the colon. Is there a simple way to get that timestamp in Java?
我想输出一个带有 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?
Also, SimpleDateFormat
cannot properly parse the example above. It throws a ParseException
.
此外,SimpleDateFormat
无法正确解析上面的示例。它抛出一个ParseException
.
// Throws a ParseException
new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZ").parse("2008-11-13T13:23:30-08:00")
采纳答案by Daniel Beck
Starting in Java 7, there's the X
pattern string for ISO8601 time zone. For strings in the format you describe, use XXX
. See the documentation.
从 Java 7 开始,有X
ISO8601 时区的模式字符串。对于您描述的格式的字符串,请使用XXX
. 请参阅文档。
Sample:
样本:
System.out.println(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
.format(new Date()));
Result:
结果:
2014-03-31T14:11:29+02:00
回答by FoxyBOA
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ss.SZ");
Is not what exactly you need?
不正是你所需要的吗?
回答by Biff
回答by Christian K.
The problem is that Z produces the time zone offset without a colon (:) as the separator.
问题是 Z 产生的时区偏移量没有冒号 (:) 作为分隔符。
回答by jjohn
From the "get it done dept," one solution is to use regexes to fix up the string after SimpleDateFormat has completed. Something like s/(\d{2})(\d{2})$/$1:$2/ in Perl.
从“完成部门”来看,一种解决方案是在 SimpleDateFormat 完成后使用正则表达式来修复字符串。类似于 Perl 中的 s/(\d{2})(\d{2})$/$1:$2/ 。
If you are even remotely interested in this, I will edit this response with the working Java code.
如果您甚至对此感兴趣,我将使用可用的 Java 代码编辑此响应。
But, yeah. I am hitting this problem too. RFC3339, I'm looking at you!
但是,是的。我也遇到了这个问题。RFC3339,我在看着你!
EDIT:
编辑:
This works for me
这对我有用
// 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)$", ":");
}
回答by crockpotveggies
I found a stray PasteBin that helped me out with the issue: http://pastebin.com/y3TCAikc
我发现了一个帮助我解决这个问题的流浪 PasteBin:http://pastebin.com/y3TCAikc
Just in case its contents later get deleted:
以防其内容稍后被删除:
// 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");
回答by Bladean Mericle
I made a InternetDateFormatclass for RFC3339.
我为 RFC3339 创建了一个InternetDateFormat类。
But source code comment is Japanese.
但是源代码注释是日语。
PS:I created English edition and refactoring a little.
PS:我创建了英文版并进行了一些重构。
回答by Rahul Ravindran
I spent quite a lot of time looking for an answer to the same issue and I found something here : http://developer.android.com/reference/java/text/SimpleDateFormat.html
我花了很多时间寻找同一问题的答案,我在这里找到了一些东西:http: //developer.android.com/reference/java/text/SimpleDateFormat.html
Suggested answer:
建议答案:
String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZZZZZ").format(new Date());
String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'h:m:ssZZZZZ").format(new Date());
If you notice I am using 5 'Z' instead of one. This gives the output with a colon in the offset like this: "2008-11-13T12:23:30-08:00". Hope it helps.
如果您注意到我使用的是 5 个“Z”而不是一个。这给出了在偏移量中带有冒号的输出,如下所示:“2008-11-13T12:23:30-08:00”。希望能帮助到你。
回答by Vijay Upadhyay
We can simply use ZonedDateTime classand DateTimeFormatter classfor this.
为此,我们可以简单地使用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));
Output: format =======> 30-03-2020T05:57:37+00:00
输出:格式 ========> 30-03-2020T05:57:37+00:00