夏令时

证明 Calendar.DST_OFFSET 和时区,还有时间相关

package com.siyuan.test.jdk;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class DSTTest {
	
	public static String format(Calendar calendar) {
		return calendar.get(Calendar.YEAR) + "-"
			+ (calendar.get(Calendar.MONTH) + 1) + "-"
			+ calendar.get(Calendar.DATE) + " "
			+ calendar.get(Calendar.HOUR_OF_DAY) + ":"
			+ calendar.get(Calendar.MINUTE) + ":"
			+ calendar.get(Calendar.SECOND);
	}
	
	public static void main(String[] args) throws ParseException {
	    
	    //证明 Calendar.DST_OFFSET 和时区,还有时间相关
	    SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Calendar calendar = Calendar.getInstance();
	    String time = "1991-04-07 00:00:00";
	    calendar.setTime(dateFmt.parse(time));
	    System.out.println(format(calendar));
	    System.out.println(calendar.getTimeZone());
	    System.out.println(calendar.get(Calendar.DST_OFFSET));
	    System.out.println("--------------------------------------------------------");
	    
	    //1.时间
	    calendar.add(Calendar.DATE, 7);
	    System.out.println(format(calendar));
	    System.out.println(calendar.getTimeZone());
	    System.out.println(calendar.get(Calendar.DST_OFFSET));
	    System.out.println("--------------------------------------------------------");
	    
	    //2.时区
	    time = "1991-04-07 00:00:00";
	    calendar.setTime(dateFmt.parse(time));
	    calendar.setTimeZone(TimeZone.getTimeZone("Europe/London"));
	    System.out.println(format(calendar));
	    System.out.println(calendar.getTimeZone());
	    System.out.println(calendar.get(Calendar.DST_OFFSET));
	    System.out.println("--------------------------------------------------------");
	    
	}

}

结果

1991-4-7 0:0:0
sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
0
--------------------------------------------------------
1991-4-14 1:0:0
sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null]
3600000
--------------------------------------------------------
1991-4-6 17:0:0
sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]]
3600000
--------------------------------------------------------

 JRE中的ZoneInfo路径

%JRE_HOME%\lib\zi

  

你可能感兴趣的:(夏令时)