Java 带时区的 SimpleDateFormat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37747467/
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
SimpleDateFormat with TimeZone
提问by Algeroth
I'm trying to format date from java.util.Date. I need this format:
我正在尝试从 java.util.Date 格式化日期。我需要这种格式:
2016-06-10T13:38:13.687+02:00
.
2016-06-10T13:38:13.687+02:00
.
How correctly convert this from standard Date format
如何正确地将其从标准日期格式转换
May 04 09:51:52 CDT 2009
?
May 04 09:51:52 CDT 2009
?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z", Locale.getDefault());
sdf.format(new Date());
This code unfortunately return value without +02:00
.
不幸的是,这段代码没有返回值+02:00
。
采纳答案by SCouto
Just turn your z to upperCase
只需将您的 z 转为大写
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.getDefault());
sdf.format(new Date());
Result: 2016-06-10T13:53:22 +0200
结果:2016-06-10T13:53:22 +0200
回答by Raja Jawahar
You have add (ZZZZZ) at the end to get this format like below
您在末尾添加了 (ZZZZZ) 以获得如下所示的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss ZZZZZ", Locale.getDefault());
回答by Opiatefuchs
You just made a simple mistake, You need to use a capital z
. What You need is:
你只是犯了一个简单的错误,你需要使用大写z
。你需要的是:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z Z", Locale.getDefault());
in addition to your lower case z
. A lower case z
gives you just the time zone, but the capital one gives you the time zone based on RFC 822.
除了你的小写z
。小写字母z
仅给出时区,但大写字母给出基于 RFC 822 的时区。
EDIT
编辑
If you not want a usual time zone, only need +2:00 without for example PST, you only need a capital Z:
如果你不想要一个通常的时区,只需要 +2:00 没有例如 PST,你只需要一个大写的 Z:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z", Locale.getDefault());
From the (very simple understandable) Docs:
来自(非常简单易懂的)文档:
z/zz/zzz:PST zzzz:Pacific Standard Time
Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
回答by TechnoTech
Add SSSZ in the format
在格式中添加 SSSZ
"yyyy-MM-dd'T'HH:mm:ss z"
=> "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
"yyyy-MM-dd'T'HH:mm:ss z"
=> "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Just change this line
只需改变这一行
Old:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z", Locale.getDefault());
sdf.format(new Date());
老的:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z", Locale.getDefault());
sdf.format(new Date());
New:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
新的:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
回答by shridutt kothari
As per the standard Java docs: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
根据标准 Java 文档:https: //docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
for getting date and time formatting of
用于获取日期和时间格式
2001-07-04T12:08:56.235-07:00
2001-07-04T12:08:56.235-07:00
You Need to use below String pattern:
您需要使用以下字符串模式:
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
“yyyy-MM-dd'T'HH:mm:ss.SSSXXX”
So with below code, you can get what you want:
所以用下面的代码,你可以得到你想要的:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault());
simpleDateFormat .format(new Date());
回答by Ole V.V.
Time for someone to provide the modern answer.
是时候有人提供现代答案了。
OffsetDateTime now = OffsetDateTime.now(ZoneId.of("Africa/Cairo"));
String formatted = now.toString();
System.out.println(formatted);
This just printed
这个刚打印
2018-02-28T16:42:59.628526+02:00
2018-02-28T16:42:59.628526+02:00
Edit: I asked for 3 decimals on the seconds, not 6.To exercise more control over the format, use an explicit formatter:
编辑:我要求秒的小数点为 3,而不是 6。要对格式进行更多控制,请使用显式格式化程序:
DateTimeFormatter formatterWithThreeDecimals
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXXXX");
String formatted = now.format(formatterWithThreeDecimals);
System.out.println(formatted);
This printed (at time of my edit):
这打印(在我编辑时):
2018-04-30T15:47:41.749+02:00
2018-04-30T15:47:41.749+02:00
Please substitute your desired time zone if it didn't happen to be Africa/Cairo. Specify ZoneId.systemDefault()
for your JVM's time zone setting. The setting can be changed any time by another part of your program or another program running in the same JVM.
如果碰巧不是非洲/开罗,请替换您想要的时区。指定ZoneId.systemDefault()
JVM 的时区设置。您的程序的其他部分或在同一 JVM 中运行的其他程序可以随时更改该设置。
Using java.time, the modern Java date and time API, in case you just want an utput that conforms with ISO 8601 and don't care how many decimals (or whether seconds are printed if they are 0), we don't need an explicit formatter at all. Your desired string is in ISO 8601 format, the standard format that the modern classes' toString
methods produce. Depending on what you need it for, the result of toString
will probably accepted.
使用 java.time,现代 Java 日期和时间 API,如果您只想要一个符合 ISO 8601 的 utput 并且不关心有多少小数(或者如果它们是 0 是否打印秒),我们不需要一个显式格式化程序。您所需的字符串采用 ISO 8601 格式,这是现代类的toString
方法生成的标准格式。根据您的需要,结果toString
可能会被接受。
The classes you were using, Date
and SimpleDateFormat
, are long outdated, and the latter in particular notoriously troublesome. I recommend you avoid them and use the modern API instead. It is so much nicer to work with.
您使用的类Date
和SimpleDateFormat
已经过时了,尤其是后者非常麻烦。我建议您避免使用它们并改用现代 API。和它一起工作要好得多。
If you got a java.util.Date
from a legacy API that you don't want to change just now, convert it to a modern Instant
and do further conversions from there. In Java 8 and later this happens like this:
如果您java.util.Date
刚刚从遗留 API获得了一个您不想更改的 API,请将其转换为现代 API,Instant
然后从那里进行进一步的转换。在 Java 8 及更高版本中,这是这样发生的:
ZonedDateTime dateTime = oldfashionedDateFromLegacyApi.toInstant()
.atZone(ZoneId.of("Africa/Cairo"));
String formatted = dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
If you are using ThreeTen Backport and/or ThreeTenABP, the conversion happens a little differently, like this:
如果您使用 ThreeTen Backport 和/或 ThreeTenABP,转换发生的方式略有不同,如下所示:
ZonedDateTime dateTime = DateTimeUtils.toInstant(oldfashionedDateFromLegacyApi)
.atZone(ZoneId.of("Africa/Cairo"));
Again, if you want close control over the output format, don't use DateTimeFormatter.ISO_OFFSET_DATE_TIME
since it will output anything from 1 through 9 decimals and may also leave out the seconds if they are 0. Instead use my formatterWithThreeDecimals
from above.
同样,如果您想密切控制输出格式,请不要使用,DateTimeFormatter.ISO_OFFSET_DATE_TIME
因为它会输出 1 到 9 位小数的任何内容,如果它们为 0,也可能会忽略秒数formatterWithThreeDecimals
。而是使用上面的my 。
Question: Can I use java.time on Android?
问题:我可以在 Android 上使用 java.time 吗?
Yes, java.time
works nicely on Android. It just requires at least Java 6.
是的,java.time
在 Android 上运行良好。它只需要至少 Java 6。
- In Java 8 and later and on newer Android devices the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
- 在 Java 8 及更高版本以及更新的 Android 设备上,现代 API 是内置的。
- 在 Java 6 和 7 中获得 ThreeTen Backport,新类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
- 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从
org.threeten.bp
子包中导入日期和时间类。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Oracle 教程:解释如何使用
java.time
. - Java 规范请求 (JSR) 310,
java.time
首先描述的地方。 - ThreeTen Backport 项目,
java.time
向 Java 6 和 7 (ThreeTen for JSR-310)的向后移植。 - ThreeTenABP, ThreeTen Backport安卓版
- 问题:如何在Android项目中使用ThreeTenABP,讲解很透彻。