Java Android 中的 ISO 8601 字符串到日期/时间对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3941357/
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
ISO 8601 String to Date/Time object in Android
提问by neildeadman
I have a string in standard ISO 8601format that contains the date/time returned from a web service like so:
我有一个标准ISO 8601格式的字符串,其中包含从 Web 服务返回的日期/时间,如下所示:
String dtStart = "2010-10-15T09:27:37Z"
How do I get this into an object such as Time or Date? I initially want to output it in a different format, but will need to do other stuff with it later (i.e. maybe use in a different format).
如何将其放入诸如时间或日期之类的对象中?我最初想以不同的格式输出它,但稍后需要用它做其他事情(即可能以不同的格式使用)。
Cheers
干杯
采纳答案by Seitaridis
String dtStart = "2010-10-15T09:27:37Z";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
This is what you are looking for. There is existing postabout this problem.
这就是你要找的。有关于这个问题的现有帖子。
回答by Jeroen Rosenberg
You can use Java's SimpleDateFormat parse methodor use JodaTime's DateTimeFormatto create a DateTimeFormatter and parse to a DateTime objectaccordingly
您可以使用Java的SimpleDateFormat的解析方法或使用JodaTime的DateTimeFormat创建创建DateTimeFormatter和解析到一个DateTime对象相应
回答by Sanzhar Askaruly
Suppose you would like to calculate time difference given the column separated values.
finish--> 15:24:04
start--> 09:27:37
Without using Dateand SimpleDateFormat, I did this way:
假设您想计算给定列分隔值的时间差。
完成--> 15:24:04
开始--> 09:27:37
不使用Date和SimpleDateFormat,我是这样做的:
String tStart = "09:27:37";
String tFinish = "15:24:04";
String[] sTimeHourMinSec = tStart.split(":");
int sHour = Integer.valueOf(sTimeHourMinSec[0]);
int sMin = Integer.valueOf(sTimeHourMinSec[1]);
int sSec = Integer.valueOf(sTimeHourMinSec[2]);
String[] fTimeHourMinSec = tFinish.split(":");
int fHour = Integer.valueOf(fTimeHourMinSec[0]);
int fMin = Integer.valueOf(fTimeHourMinSec[1]);
int fSec = Integer.valueOf(fTimeHourMinSec[2]);
int diffTotSec = (fHour - sHour) * 3600 + (fMin - sMin) * 60 + (fSec - sSec);
int diffHours = diffTotSec / 3600;
int diffMins = (diffTotSec % 3600) / 60;
int diffSecs = (diffTotSec % 3600) % 60;
System.out.println("Difference: " + diffHours + " h " + diffMins + " m " + diffSecs + " sec");
回答by Ole V.V.
This question was asked in 2010, and back then it was correct that either SimpleDateFormat
or Joda-Time would be the tools you should use. It's quite a while ago now. Today use
这个问题是在 2010 年提出的,当时SimpleDateFormat
您应该使用或 Joda-Time是正确的工具。现在已经有一段时间了。今天使用
Instant iStart = Instant.parse(dtStart);
Yes, it's this simple. Your string is in ISO 8601 format, and the classes from java.time
, the modern Java date and time API, parse ISO 8601 without any explicit formatter. Instant
is just one of those classes.
是的,就是这么简单。您的字符串采用 ISO 8601 格式,来自java.time
现代 Java 日期和时间 API的类在没有任何显式格式化程序的情况下解析 ISO 8601。Instant
只是这些课程之一。
Edit: Question: requires android API 26 - what about supporting older versions?
编辑:问题:需要 android API 26 - 支持旧版本怎么样?
Yes, java.time
works nicely on older and newer Android devices. It just requires at least Java 6.
是的,java.time
在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6。
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
- 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。
- 在 Java 6 和 7 中获得 ThreeTen Backport,新类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
- 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从
org.threeten.bp
子包中导入日期和时间类。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use
java.time
. - Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Wikipedia article: ISO 8601
- Oracle 教程:解释如何使用
java.time
. - Java 规范请求 (JSR) 310,
java.time
首先描述的地方。 - ThreeTen Backport 项目,
java.time
向 Java 6 和 7 (ThreeTen for JSR-310)的向后移植。 - ThreeTenABP, ThreeTen Backport安卓版
- 问题:如何在Android项目中使用ThreeTenABP,讲解很透彻。
- 维基百科文章:ISO 8601
回答by pram
If you use current java.time
or threetenbp
simply parse it like this:
如果您使用 currentjava.time
或threetenbp
只是像这样解析它:
ZonedDateTime dt = ZonedDateTime.parse(dtStart);
Now you can access date and time values such as year, month, hour, etc.
现在您可以访问日期和时间值,例如年、月、小时等。