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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:25:24  来源:igfitidea点击:

Current Date Time in " YYYY-MM-DDTHH:MM.000z" (ISO 8601) format in Android

javaandroidiso8601

提问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.

您要求将ISO 8601格式设置为UTC时区。

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 Instantclass 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类如很多讨论ZoneIdZonedDateTimeOffsetDateTime

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 类?

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 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多