Java 如何将 2018-04-10T04:00:00.000Z 字符串转换为 DateTime?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49752149/
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
How do I convert 2018-04-10T04:00:00.000Z string to DateTime?
提问by Miguel Barra
I get a date from a JSON API which looks like this "2018-04-10T04:00:00.000Z". I want to convert it in order to obtain a Date or String object and get something like "01-04-2018" that its "dd-MM-YYYY". How can I do it?
我从 JSON API 得到一个日期,它看起来像这样“2018-04-10T04:00:00.000Z”。我想转换它以获得日期或字符串对象并获得类似“01-04-2018”的“dd-MM-YYYY”。我该怎么做?
采纳答案by Tamas Rev
Update: Using DateTimeFormat
, introduced in java 8:
更新:使用DateTimeFormat
,在 java 8 中引入:
The idea is to define two formats: one for the input format, and one for the output format. Parse with the input formatter, then format with the output formatter.
这个想法是定义两种格式:一种用于输入格式,一种用于输出格式。使用输入格式化程序进行解析,然后使用输出格式化程序进行格式化。
Your input format looks quite standard, except the trailing Z
. Anyway, let's deal with this: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
. The trailing 'Z'
is the interesting part. Usually there's time zone data here, like -0700
. So the pattern would be ...Z
, i.e. without apostrophes.
您的输入格式看起来很标准,除了尾随Z
. 无论如何,让我们处理这样的:"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
。尾随'Z'
是有趣的部分。通常这里有时区数据,比如-0700
. 所以模式将是...Z
,即没有撇号。
The output format is way more simple: "dd-MM-yyyy"
. Mind the small y
-s.
输出格式更简单:"dd-MM-yyyy"
. 注意小y
-s。
Here is the example code:
这是示例代码:
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd-MM-yyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse("2018-04-10T04:00:00.000Z", inputFormatter);
String formattedDate = outputFormatter.format(date);
System.out.println(formattedDate); // prints 10-04-2018
Original answer - with old API SimpleDateFormat
原始答案 - 使用旧 API SimpleDateFormat
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = inputFormat.parse("2018-04-10T04:00:00.000Z");
String formattedDate = outputFormat.format(date);
System.out.println(formattedDate); // prints 10-04-2018
回答by Deb
Using Date pattern yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
and Java 8 you could do
使用日期模式yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
和 Java 8 你可以做
String string = "2018-04-10T04:00:00.000Z";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date);
Update: For pre 26 use Joda time
更新:对于 26 岁之前使用 Joda 时间
String string = "2018-04-10T04:00:00.000Z";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
LocalDate date = org.joda.time.LocalDate.parse(string, formatter);
In app/build.gradle file, add like this-
在 app/build.gradle 文件中,像这样添加-
dependencies {
compile 'joda-time:joda-time:2.9.4'
}