Java 打印美国太平洋时间

时区打印

package test.algorithm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class TestAlgorithm
{
    
    
    private static Logger LOGGER = LoggerFactory.getLogger(TestAlgorithm.class);

    public static void main(String[] args) {
    
    
        DateFormat shanghai = PrintShangHaiTimeZone();
        DateFormat psd = PrintAmericaTimeZone();
    }

    public static DateFormat PrintShangHaiTimeZone(){
    
    
        String patternStr = "yyyy-MM-dd HH:mm:ss";
        Date bjDate = new Date();
        TimeZone shanghaiTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
        DateFormat shanghaiDateFormat = new SimpleDateFormat(patternStr);
        shanghaiDateFormat.setTimeZone(shanghaiTimeZone);
        System.out.println("这是北京时间:" + shanghaiDateFormat.format(bjDate));
        System.out.println("时间戳:" + shanghaiDateFormat.getCalendar().getTimeInMillis());

        return shanghaiDateFormat;
    }

    public static DateFormat PrintAmericaTimeZone() {
    
    
        String patternStr = "yyyy-MM-dd HH:mm:ss";
        Date bjDate = new Date();
                
        TimeZone newYorkTimeZone = TimeZone.getTimeZone("America/Los_Angeles");
        // or
        // TimeZone newYorkTimeZone = TimeZone.getTimeZone("PST");
        DateFormat newYorkDateFormat = new SimpleDateFormat(patternStr);
        newYorkDateFormat.setTimeZone(newYorkTimeZone);
        System.out.println("这是美国太平洋时间:" + newYorkDateFormat.format(bjDate));
        System.out.println("时间戳:" + newYorkDateFormat.getCalendar().getTimeInMillis());

        return newYorkDateFormat;
    }
}

输出:
在这里插入图片描述

注意有个夏令时,比如夏令时的时候,美国太平洋时间和北京时间相差是 15个小时;非夏令时,恢复正常的16个小时。

JVM 设置时区

-Duser.timezone=PST

PST: 美国太平洋时区(或者 America/Los_Angeles)

附录

[1] java时区转换的理解及示例详解
[2] 彻底弄透Java处理GMT/UTC日期时间

猜你喜欢

转载自blog.csdn.net/WGYHAPPY/article/details/129887002
今日推荐