时区内发生令时,时间戳基准时间不会改变
private static void printDate(long now) {
// 美国时间
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println("time=" + now + " " + new Date(now));
//System.out.println("============================================================");
}
// String time="2018-09-29 16:39:00";
private static long getTime(String time){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
long unixtime = 0;
try {
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
date = format.parse(time);
unixtime = date.getTime();
System.out.println("parseTo time: " + unixtime);
} catch (ParseException e) {
e.printStackTrace();
}
return unixtime;
}
public static void main( String[] args )
{
System.out.println("-----------冬令时-----------");
long abc1 = 1572771600000L - 1000L;
printDate(abc1);
abc1 = 1572771600000L;
printDate(abc1);
System.out.println("----------夏令时------------");
long abc = 1583661600000L - 1000L;
printDate(abc);
abc = 1583661600000L;
printDate(abc);
long test = getTime("2020-03-08 03:30:00");
printDate(test);
long diff = abc - test;
System.out.println("----------------------");
}