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

原文地址:https://www.cnblogs.com/passedbylove/p/12209989.html