英文:
How to generate localized datetime from "20201023T200457Z" and from "EEE MMM dd HH:mm:ss zzz yyyy"?
问题
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.getDefault());
Date startGMTInput = df.parse(start);
TimeZone gmtZone = TimeZone.getTimeZone("GMT");
df.setTimeZone(gmtZone);
String convertedDate = df.format(startGMTInput);
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
TimeZone localTimeZone = TimeZone.getDefault();
outputFormat.setTimeZone(localTimeZone);
String finalResult = outputFormat.format(startGMTInput);
Log.e(TAG, "start: " + finalResult);
英文:
I have this start datetime: "20201023T200457Z" (it seem to be UTC0000)
how can I convert/generate it with this "yyyyMMdd HH:mm:ss" pattern in local time on a mobile?
I get this result: Fri Oct 23 20:04:57 GMT+02:00 2020
with this code:
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.getDefault());
Date startGMTInput = df.parse(start);
Log.e(TAG, "start: " + startGMTInput.toString());// -> Fri Oct 23 20:04:57 GMT+02:00 2020
But my target is to get: 2020-10-23 22:04:57 //because I'm in GMT+2 timezone
答案1
得分: 1
## java.time 通过解糖或 ThreeTenABP 进行操作
考虑在处理日期和时间时使用现代的 Java 日期和时间 API - java.time。首先,让我们定义所需的格式化器:
private static final DateTimeFormatter inputFormatter
= DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmssX");
private static final DateTimeFormatter outputFormatter
= DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
(`DateTimeFormatter` 是线程安全的,因此我们可以安全地将它们声明为静态变量。)需要显式进行时区转换:
String startString = "20201023T200457Z";
Instant start = inputFormatter.parse(startString, Instant.FROM);
String target = start.atZone(ZoneId.systemDefault()).format(outputFormatter);
System.out.println(target);
在我的时区中输出(目前偏移量为 +02:00,与您的时区类似):
> 20201023 22:04:57
## 问题:java.time 是否需要 Android API 级别 26?
java.time 在旧版和新版 Android 设备上都能正常使用。它只需要至少 **Java 6**。
- 在 Java 8 及更高版本以及较新的 Android 设备(从 API 级别 26 开始),这个现代化的 API 已经内置。只需在这种情况下使用方法引用 `Instant::from`,而不是常量 `Instant.FROM`。
- 在非 Android 环境下的 Java 6 和 7 中,请使用 ThreeTen Backport,这是现代类的后移版本(ThreeTen 用于 JSR 310;请参阅底部的链接)。
- 在旧版 Android 中,您可以使用解糖或 ThreeTen Backport 的 Android 版本,称为 ThreeTenABP。在后一种情况下,请确保从 `org.threeten.bp` 及其子包中导入日期和时间类。
## 链接
- [Oracle 教程:日期时间](https://docs.oracle.com/javase/tutorial/datetime/) 解释了如何使用 java.time。
- [Java 规范请求 (JSR) 310](https://jcp.org/en/jsr/detail?id=310),最初描述了 `java.time`。
- [ThreeTen Backport 项目](http://www.threeten.org/threetenbp/),将 `java.time` 后移至 Java 6 和 7 的版本(ThreeTen 用于 JSR-310)。
- [通过解糖可用的 Java 8+ API](https://developer.android.com/studio/write/java8-support-table)
- [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP),ThreeTen Backport 的 Android 版本
- [问题:如何在 Android 项目中使用 ThreeTenABP](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project),提供了非常详细的解释。
英文:
java.time either through desugaring or ThreeTenABP
Consider using java.time, the modern Java date and time API, for your date and time work. Let’s first define the formatters we need:
private static final DateTimeFormatter inputFormatter
= DateTimeFormatter.ofPattern("uuuuMMdd'T'HHmmssX");
private static final DateTimeFormatter outputFormatter
= DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
( DateTimeFormatter
is thread-safe, so we can safely declare them static.) Do the time zone conversion explicitly:
String startString = "20201023T200457Z";
Instant start = inputFormatter.parse(startString, Instant.FROM);
String target = start.atZone(ZoneId.systemDefault()).format(outputFormatter);
System.out.println(target);
Output in my time zone (currently at offset +02:00 like yours):
> 20201023 22:04:57
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in. Only in this case use the method reference
Instant::from
instead of the constantInstant.FROM
. - In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining 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). - Java 8+ APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
答案2
得分: 0
将您的时区设置为GMT+2,然后进行任何日期操作。
isoFormat.setTimeZone(TimeZone.getTimeZone("GMT+2"));
英文:
Set your timezone to GMT+2 before any date operations.
isoFormat.setTimeZone(TimeZone.getTimeZone("GMT+2"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论