[英]Android convert gmt time to readable date
In android I would like to convert "2015-11-10T17:00:00-0500" to a readable date format such as :在android中,我想将“2015-11-10T17:00:00-0500”转换为可读的日期格式,例如:
Nov 31, 2015 2015 年 11 月 31 日
Nov 31, 2015 4:00:00 PM 2015 年 11 月 31 日下午 4:00:00
4:00:00 PM 12:00:00 AM下午 4:00:00 中午 12:00:00
what would be the best way to do this, I have tried and failed using the Date method最好的方法是什么,我已经尝试过使用 Date 方法但失败了
Just use SimpleDateFormat, something like this: 只需使用SimpleDateFormat,如下所示:
String time = "2015-11-10T17:00:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm:ss dd.MM.yyyy");
try {
Date date = dateFormat.parse(time);
String out = dateFormat2.format(date);
Log.e("Time", out);
} catch (ParseException e) {
}
The legacy date-time API ( java.util
date-time types and their formatting type, SimpleDateFormat
) is outdated and error-prone.遗留日期时间 API(
java.util
日期时间类型及其格式类型SimpleDateFormat
)已过时且容易出错。 It is recommended to stop using it completely and switch to java.time
, the modern date-time API * .建议完全停止使用它并切换到
java.time
, 现代日期时间 API * 。
Your date-time string has a timezone offset of -0500
ie the corresponding date-time in UTC is 2015-11-10T22:00:00Z
where Z
stands for Zulu and is the timezone designator for zero-timezone offset ie Etc/UTC
timezone which has the timezone offset of +00:00
hours.您的日期时间字符串的时区偏移量为
-0500
即 UTC 中的相应日期时间为2015-11-10T22:00:00Z
,其中Z
代表祖鲁语,是零时区偏移的时区指示符,即Etc/UTC
时区它的时区偏移为+00:00
小时。
The type which represents date-time along with the timezone offset is OffsetDateTime
.表示日期时间和时区偏移的类型是
OffsetDateTime
。 In order to format it into different patterns, you need a DateTimeFormatter
.为了将其格式化为不同的模式,您需要一个
DateTimeFormatter
。
Demo:演示:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d'T'H:m:sXX", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse("2015-11-10T17:00:00-0500", dtfInput);
DateTimeFormatter dtfOutput1 = DateTimeFormatter.ofPattern("EEE dd, uuuu", Locale.ENGLISH);
DateTimeFormatter dtfOutput2 = DateTimeFormatter.ofPattern("EEE dd, uuuu h:mm:ss a", Locale.ENGLISH);
DateTimeFormatter dtfOutput3 = DateTimeFormatter.ofPattern("h:mm:ss a", Locale.ENGLISH);
String output1 = dtfOutput1.format(odt);
String output2 = dtfOutput2.format(odt);
String output3 = dtfOutput3.format(odt);
System.out.println(output1);
System.out.println(output2);
System.out.println(output3);
}
}
Output:输出:
Tue 10, 2015
Tue 10, 2015 5:00:00 PM
5:00:00 PM
Learn more about the modern date-time API * from Trail: Date Time .从Trail: Date Time 中了解有关现代日期时间 API * 的更多信息。
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project . * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和您的 Android API 工作级别仍然不符合 Java-8,请检查 通过 desugaring和How to use ThreeTenABP in Android Project 可用的 Java 8+ APIs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.