java中將RFC1123日期時間格式化


JDK8新的日期時間類轉換方法:

package com.example;

import org.junit.Test;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class TestRFC1123 {

    String rfc1123Times[] = new String[]
            {
                    "Sat, 08 Jan 2000 18:31:41 GMT",
                    "Wed, 27 Sep 2006 21:36:45 +0200"
            };

    /***
     * jdk 8 new date/time
     */
    @Test
    public void testParse()
    {
        String rfc1123Time;
        for (int i = 0; i < rfc1123Times.length; i++) {
            rfc1123Time = rfc1123Times[i];

            DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
            Instant instant = Instant.from(formatter.parse(rfc1123Time));
            System.out.println("國際時間:"+instant);//說法可能有錯

            ZonedDateTime zdt =
                    ZonedDateTime.parse(
                            rfc1123Time ,
                            DateTimeFormatter.RFC_1123_DATE_TIME
                    );

            ZoneId zone = ZoneId.of( "Asia/Shanghai" );  // Or "Asia/Kolkata", etc.
            ZonedDateTime zdtMontreal = zdt.withZoneSameInstant( zone );

            System.out.println("帶時區的時間:"+zdt+",北京時間:"+zdtMontreal);
        }
    }
}

 

 參考來源:How to Convert RFC-1123 date-time formatter, to local time


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM