简体   繁体   English

转换 GMT 模式日期时间

[英]Convert GMT pattern date time

how can I parse this DateTime format?如何解析这种 DateTime 格式?

Wed Feb 03 2021 08:40:44 GMT+08:00 2021 年 2 月 3 日星期三 08:40:44 GMT+08:00

to

Wed 03 Feb 2021 08:40:44 am 2021 年 2 月 3 日星期三 08:40:44 am

java.time java.time

I recommend you do it using the the modern date-time API * .我建议您使用现代日期时间 API *来执行此操作。 The legacy date-time API ( java.util date-time types and their formatting API, SimpleDateFormat ) are outdated and error-prone.旧的日期时间 API ( java.util日期时间类型及其格式 API, SimpleDateFormat ) 已过时且容易出错。 It is recommended to stop using them completely and switch to java.time , the modern date-time API.建议完全停止使用它们并切换到java.time ,现代日期时间 API。

Solution using modern date-time API:使用现代日期时间 API 的解决方案:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String args[]) {
        String dateStr = "Wed Feb 03 2021 08:40:44 GMT+08:00";

        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("EEE MMM d u H:m:s O", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(dateStr, dtfInput);

        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("EEE dd MMM uuuu hh:mm:ss a", Locale.UK);
        String formatted = dtfOutput.format(zdt);
        System.out.println(formatted);
    }
}

Output: Output:

Wed 03 Feb 2021 08:40:44 am

In case you need an object of java.util.Date from this object of ZonedDateTime , you can so as follows:如果您需要 object 的java.util.Date来自 object 的ZonedDateTime ,您可以执行以下操作:

Date date = Date.from(zdt.toInstant());

Learn more about the the modern date-time API * from Trail: Date Time .Trail: Date Time了解有关现代日期时间 API *的更多信息。

Solution using the legacy API:使用旧版 API 的解决方案:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String args[]) throws ParseException {
        String dateStr = "Wed Feb 03 2021 08:40:44 GMT+08:00";

        SimpleDateFormat sdfInput = new SimpleDateFormat("EEE MMM d y H:m:s z", Locale.ENGLISH);
        Date date = sdfInput.parse(dateStr);

        SimpleDateFormat sdfOutput = new SimpleDateFormat("EEE dd MMM yyyy hh:mm:ss a", Locale.UK);
        String formatted = sdfOutput.format(date);
        System.out.println(formatted);
    }
}

Output: Output:

Wed 03 Feb 2021 12:40:44 am

* 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 . * 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级别仍然不符合 Java-8,请检查Java 8+ API 可通过脱糖如何在 Android 项目中使用 ThreeTenABP

Have you taken a look at the SimpleDateFormat class ( https://developer.android.com/reference/java/text/SimpleDateFormat )?您是否看过 SimpleDateFormat class ( https://developer.android.com/reference/java/text/SimpleDateFormat )? It should be able to display the date any way you like.它应该能够以您喜欢的任何方式显示日期。

Edit: Please see Arvind Kumar Avinash's answer instead.编辑:请参阅 Arvind Kumar Avinash 的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM