码迷,mamicode.com
首页 > 编程语言 > 详细

java中将RFC1123日期时间格式化

时间:2020-01-18 21:40:01      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:nes   code   sha   convert   shang   lin   jdk8   com   cal   

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

java中将RFC1123日期时间格式化

标签:nes   code   sha   convert   shang   lin   jdk8   com   cal   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!