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
Convert "2018-02-02T06:54:57.744Z" String to date in Android
提问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.744Z
want 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 SimpleDateFormat
but 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
Z
is 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
输出
回答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 类。
Instant
is 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.Date
class 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- Backport和ThreeTenABP项目。
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 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android,ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。