Java 在 Android 中将“2018-02-02T06:54:57.744Z”字符串转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48594916/
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-12 02:53:28  来源:igfitidea点击:

Convert "2018-02-02T06:54:57.744Z" String to date in Android

javaandroidiso8601

提问by Zulqarnain

Basically my date string is ISO-8601 date string, So I searched for converting ISO-8601 date string to date, but the solutions are lengthy. Here are the details.

基本上我的日期字符串是 ISO-8601 日期字符串,所以我搜索了将 ISO-8601 日期字符串转换为日期的方法,但解决方案很长。这是详细信息。

I have a string 2018-02-02T06:54:57.744Zwant to convert it to Date but I am getting error:

我有一个字符串2018-02-02T06:54:57.744Z想将其转换为 Date 但出现错误:

java.text.ParseException: Unparseable date: "2018-02-02T06:54:57.744Z"

I am using following technique to do that but no success:

我正在使用以下技术来做到这一点,但没有成功:

SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
        parser.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date parsed = parser.parse(utcDateString);
        return parsed;

I also use following patterns with SimpleDateFormatbut no success:

我也使用以下模式SimpleDateFormat但没有成功:

yyyy-MM-dd'T'HH:mm:ss.SSSZ
yyyy-MM-dd HH:mm:ss.S
yyyy-MM-dd'T'HH:mm:ssX
yyyy-MM-dd'T'HH:mm'Z'

any solution to this problem.

这个问题的任何解决方案。

采纳答案by Ali Behzadian Nejad

Zis timezone. You have to add timezone at the end of your date string or simply remove it from format: yyyy-MM-dd'T'HH:mm:ss

Z是时区。您必须在日期字符串的末尾添加时区或简单地从格式中删除它:yyyy-MM-dd'T'HH:mm:ss

A correct date string with timezone is something like this: 2018-02-02T06:54:57.744+0200

带有时区的正确日期字符串是这样的: 2018-02-02T06:54:57.744+0200

回答by Ratilal Chopda

Try this

尝试这个

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy");

Date d = null;
try 
{
   d = input.parse("2018-02-02T06:54:57.744Z");
} 
catch (ParseException e) 
{
   e.printStackTrace();
}
String formatted = output.format(d);
Log.i("DATE", "" + formatted);

OUTPUT

输出

enter image description here

在此处输入图片说明

回答by Basil Bourque

tl;dr

tl;博士

Instant.parse( “2018-02-02T06:54:57.744Z” ) 

java.time

时间

The modern approach uses java.time classes.

现代方法使用 java.time 类。

Instantis the replacement for java.util.Date, representing a moment on the timeline in UTC.

Instant是 的替代品java.util.Date,代表 UTC 时间线上的一个时刻。

Your input string happens to comply with the ISO 8601 standard. The java.time classes use standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

您的输入字符串恰好符合 ISO 8601 标准。java.time 类在解析/生成字符串时默认使用标准格式。所以不需要指定格式模式。

Instant instant = Instant.parse( “2018-02-02T06:54:57.744Z” ) ;

Best to avoid the troublesome java.util.Dateclass entirely. But if you insist, convert using new methods added to the old classes.

最好java.util.Date完全避免麻烦的课程。但是,如果您坚持,请使用添加到旧类中的新方法进行转换。

Date d = Date.from( instant ) ;

For earlier Android, see the ThreeTen-Backportand ThreeTenABPprojects.

对于早期的 Android,请参阅ThreeTen- BackportThreeTenABP项目。



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