Java print US Pacific Time

time zone printing

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;
    }
}

Output:
Insert image description here

Note that there is daylight saving time. For example, during daylight saving time, the difference between U.S. Pacific Time and Beijing time is 15 hours; outside of daylight saving time, it returns to the normal 16 hours.

JVM set time zone

-Duser.timezone=PST

PST: United States Pacific Time Zone (or America/Los_Angeles)

appendix

[1] Understanding and detailed examples of Java time zone conversion
[2] Thoroughly understand Java's processing of GMT/UTC date and time

Guess you like

Origin blog.csdn.net/WGYHAPPY/article/details/129887002