簡體   English   中英

Joda時間-使用Uda版本1.2.1.1將UTC日期時間轉換為日期

[英]Joda Time - Convert UTC DateTime to Date - Using a Version 1.2.1.1 of Joda

大家,早安。

我想幫助您了解如何使用1.2.1.1版本的Joda Time將org.joda.time.DateTime轉換為java.util.Date。

為什么選擇喬達1.2.1.1? 因為當前我只能“不幸地”使用此版本的Joda。

我的測試>

    System.out.println("JODA Version : 2.8.2 - UTC TIME to Date " + new DateTime().withZone(DateTimeZone.UTC).toLocalDateTime().toDate());;
    System.out.println("JODA Version : 1.2.1.1 - UTC TIME to Date " + new DateTime().withZone(DateTimeZone.UTC).toDate());;


JODA Version : 2.8.2 - UTC TIME to Date Fri Sep 18 17:34:36 BRT 2015
JODA Version : 1.2.1.1 - UTC TIME to Date Fri Sep 18 14:34:36 BRT 2015

我的問題是,在1.2.1.1版中,日期位於我的本地設置上,而在此版本中,沒有toLocalDateTime()方法。

我想請您的幫助和經驗來發現最佳實踐,以便在JODA版本:1.2.1.1中執行此轉換。

在舊版本的JODA中,如何在UTC中將時間轉換為小時:分鍾:秒?

我做了很多研究,發現有人說這樣做會是一個好習慣嗎?

public  static Date converterDateTimeToUTCDate(final DateTime dateTime) throws ParseException {
    DateTime dat = dateTime.withZone(DateTimeZone.UTC);
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").parse(dat.toString("yyyy-MM-dd HH:mm:ss:SSS"));
}

public static void main(String[] args) throws ParseException {
    System.out.println("TODO DATE UTC TIME : " + new DateTime().withZone(DateTimeZone.UTC));
    System.out.println("TODO DATE Convertion Direct Date: " + new DateTime().withZone(DateTimeZone.UTC).toDate());

    Date converterUTCDateTimeToDate = converterDateTimeToUTCDate(new DateTime());

    System.out.println("TODO DATE UTC with Parse : " + converterUTCDateTimeToDate);

}

結果:

TODO DATE UTC TIME : 2015-09-18T22:33:57.353Z
TODO DATE Convertion Direct Date: Fri Sep 18 19:33:57 BRT 2015
TODO DATE UTC with Parse : Fri Sep 18 22:33:57 BRT 2015

編輯為什么選擇喬達1.2.1.1? 因為當前我只能“不幸地”使用此版本的Joda。

我在一家公司中工作,該公司需要很長時間才能在項目中更改API版本,而我的項目沒有此等待時間,因此無法使用新版本

更新:

我看了一下,java Date沒有TimeZone,這個BRT是在類的toString()方法中從本地計算機捕獲的,我可以認為轉換是正確的嗎?

更新2

我為示例編輯了答案示例:

看到:

package joda;

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

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

public class ConverterDateTimeToDate {
    public static final String BASE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS z";

    public static void main(String[] args) {

        // Different display time zones

        SimpleDateFormat formatUTC = new SimpleDateFormat( BASE_FORMAT );
        formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));

        SimpleDateFormat formatBrazil = new SimpleDateFormat( BASE_FORMAT );
        formatBrazil.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));

        SimpleDateFormat formatCentralEurope = new SimpleDateFormat( BASE_FORMAT );
        formatCentralEurope.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"));

        // Get a date in UTC

        String dateString = "2015-09-19 10:45:00.000 UTC";
        Date javaDate = null;
        try {
            DateTime dateTime = new DateTime().withZone(DateTimeZone.forID("America/Mexico_City"));
System.out.println("MEX TIME IN JODA : " + dateTime); //new Test
            System.out.println("MEX TIME IN JODA CONVERTER : " + dateTime.toDate()); // new Test
            System.out.println("Now in MEX Time Zone DateTime : " + dateTime);

            javaDate = formatUTC.parse(dateTime.toString(BASE_FORMAT));
        } catch (ParseException e) {
            e.printStackTrace(); // Shouldn't happen.
        }

        // Now let's print it in various time zones. It's the same date - 10:45 in UTC!

        System.out.println("In UTC:             " + formatUTC.format(javaDate));
        System.out.println("In Brazil:          " + formatBrazil.format(javaDate));
        System.out.println("In the Netherlands: " + formatCentralEurope.format(javaDate));

    }
}

我的輸出結果:在UTC中輸入:2015-09-19 12:10:56.731 CDT ,我的轉換問題嗎? 因為我在系統中的DateTime來了

我的輸出:

MEX TIME IN JODA : 2015-09-21T21:17:46.781-05:00
MEX TIME IN JODA CONVERTER : Mon Sep 21 23:17:46 BRT 2015
Now in MEX Time Zone DateTime : 2015-09-21T21:17:46.781-05:00
In UTC:             1732-01-11 02:17:46.781 UTC
In Brazil:          1732-01-10 23:17:46.781 BRT
In the Netherlands: 1732-01-11 03:17:46.781 CET

正確的方法是使用toDate()

即使在較舊的JodaTime中, DateTime類中的方法也是正確的。 這里是一個解釋:


有關java.util.Date如何工作的說明

您似乎對java.util.Date有誤解。 它不包含時區。 它表示自1970年1月以來的時間偏移量, UTC時間00:00。

當您打印Date對象時,您的JVM將使用您的默認時區並向您顯示該時區的Date 因此,在打印日期時,如果要在其他時區查看日期,則應始終使用DateFormat對象。 例如,如果要查看UTC日期,則必須使用其時區設置為UTC的日期格式。 這是一個例子:

public static final String BASE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS z";

public static void main(String[] args) {

    // Different display time zones

    SimpleDateFormat formatUTC = new SimpleDateFormat( BASE_FORMAT );
    formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));

    SimpleDateFormat formatBrazil = new SimpleDateFormat( BASE_FORMAT );
    formatBrazil.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));

    SimpleDateFormat formatCentralEurope = new SimpleDateFormat( BASE_FORMAT );
    formatCentralEurope.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam"));

    // Get a date in UTC

    String dateString = "2015-09-19 10:45:00.000 UTC";
    Date javaDate = null;
    try {
        javaDate = formatUTC.parse(dateString);
    } catch (ParseException e) {
        e.printStackTrace(); // Shouldn't happen.
    }

    // Now let's print it in various time zones. It's the same date - 10:45 in UTC!

    System.out.println("In UTC:             " + formatUTC.format(javaDate));
    System.out.println("In Brazil:          " + formatBrazil.format(javaDate));
    System.out.println("In the Netherlands: " + formatCentralEurope.format(javaDate));

}

該程序的輸出為:

In UTC:             2015-09-19 10:45:00.000 UTC
In Brazil:          2015-09-19 07:45:00.000 BRT
In the Netherlands: 2015-09-19 12:45:00.000 CEST

您可以看到我們打印了相同的日期 ,並且根據格式的時區顯示的日期有所不同。


從Joda TimeStamp正確轉換為java.util.Date

對於Joda的DateTime對象,邏輯相同,但更復雜,因為它還包括時區,盡管並非用於所有操作。 在內部,它也表示與UTC的偏移量。

當使用其toDate()方法時,它依賴於UTC的內部偏移量,因此,它根據java.util.Date協定為您提供了正確的Date對象

讓我們演示一下,通過將上述程序中獲取日期的方式替換為:

DateTime jodaDateTime = new DateTime( 2015, 9, 19, 10, 45, 0, 0, DateTimeZone.UTC);

Date javaDate = jodaDateTime.toDate();

現在,運行與以前相同的打印,我們再次得到:

In UTC:             2015-09-19 10:45:00.000 UTC
In Brazil:          2015-09-19 07:45:00.000 BRT
In the Netherlands: 2015-09-19 12:45:00.000 CEST

因此,您可以看到,如果正確設置了Joda DateTime ,則使用其toDate將為您提供正確的Date對象。


顯示使用toLocalDateTime()是錯誤的

現在,如果我們使用第一種方法,那么您認為正確的方法(僅在Joda 2.0及更高版本中存在),我們將得到什么?

我們將代碼更改為:

DateTime jodaDateTime = new DateTime( 2015, 9, 19, 10, 45, 0, 0, DateTimeZone.UTC);
Date javaDate = jodaDateTime.toLocalDateTime().toDate();

Joda DateTime與以前相同,我們只是添加了僅在Joda 2中存在的toLocalDateTime()

假設系統上的默認時區是BRT,您將得到以下結果:

In UTC:             2015-09-19 13:45:00.000 UTC
In Brazil:          2015-09-19 10:45:00.000 BRT
In the Netherlands: 2015-09-19 15:45:00.000 CEST

這顯然不是正確的日期! toLocalDateTime()部分獲取了您的本地時間偏移,並將其添加到日期中以形成“本地”日期。 只要您停留在Joda時間構造內,這就很好,但是因為它設置了與UTC不正確的偏移量,所以違反了java.util.Date的約定。


結論

在舊的Joda中使用的舊方法是從org.joda.time.DateTime獲取適當的java.util.Dateorg.joda.time.DateTime 但是您在打印java.util.Date必須非常小心,因為默認情況下它將在您的默認時區打印。

最后一個建議:如果您想在公司中開始升級過程,請不要擔心Joda的時間。 請他們開始將系統升級到Java 8,這是Oracle唯一維護的Java版本。 Java 8包括適當的日期/時間庫,Joda的創建者建議改用該庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM