Java Android 中“YYYY-MM-DDTHH:MM.000z”(ISO 8601)格式的当前日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20225425/
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
Current Date Time in " YYYY-MM-DDTHH:MM.000z" (ISO 8601) format in Android
提问by user3037815
Hello i am very new to android Development , working on a Android Code right now ,I need to Get the current date and time in this Format
您好,我是 Android 开发的新手,现在正在处理 Android 代码,我需要以这种格式获取当前日期和时间
YYYY-MM-DDTHH:MM.000z
For Example 2013-11-26T03:16:00.000Z
例如 2013-11-26T03:16:00.000Z
Any Help will be really Appreciated
任何帮助将不胜感激
采纳答案by Matt N.
There is documentation readily available for Android's SimpleDateFormat class at:
Android 的 SimpleDateFormat 类有现成的文档:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
http://developer.android.com/reference/java/text/SimpleDateFormat.html
Also appears to have been answered before.Among other places.
回答by Joel Fernandes
Use SimpleDateFormat sd= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
用 SimpleDateFormat sd= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
回答by Basil Bourque
tl;dr
tl;博士
You are asking for the ISO 8601format set to UTCtime zone.
Instant.now().toString()
2018-01-25T22:50:24.702645Z
2018-01-25T22:50:24.702645Z
java.time
时间
The java.time classes use the standard ISO 8601 formats by default when parsing or generating strings.
java.time 类在解析或生成字符串时默认使用标准 ISO 8601 格式。
The Instant
class represents a moment on the timeline in UTCwith a resolution of nanoseconds(up to nine (9) digits of a decimal fraction).
该Instant
级表示时间轴上的时刻UTC,分辨率为纳秒(最多小数的9个位数)。
Get the current moment in UTC:
获取 UTC 中的当前时刻:
Instant instant = Instant.now() ;
To generate a string in standard ISO 8601 format:
要生成标准 ISO 8601 格式的字符串:
String output = instant.toString() ; // Generate string in standard format.
Going the other direction, parsing such a string.
走向另一个方向,解析这样的字符串。
Instant instant = Instant.parse( "2013-11-26T03:16:00.000Z" );
Search Stack Overflow for much discussion of java.time classes such as ZoneId
, ZonedDateTime
, and OffsetDateTime
.
搜索栈溢出了java.time类如很多讨论ZoneId
,ZonedDateTime
和OffsetDateTime
。
Joda-Time
乔达时间
Update: The Joda-Timeproject is now in maintenance mode, with the team advising migration to the java.timeclasses.
更新:Joda-Time项目现在处于维护模式,团队建议迁移到java.time类。
ISO 8601 formats are the default for the Joda-Time2.3 library.
ISO 8601 格式是Joda-Time2.3 库的默认格式。
System.out.println( "Now: " + new org.joda.time.DateTime( org.joda.time.DateTimeZone.UTC ) );
When run…
运行时…
Now: 2013-11-26T20:25:12.014Z
About java.time
关于 java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8and SE 9and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABPproject adapts ThreeTen-Backport(mentioned above) for Android specifically.
- See How to use ThreeTenABP….
- Java SE 8和SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 所述ThreeTenABP项目适应ThreeTen-反向移植(上述)为Android特异性。
- 请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。