我在 IST 中有一个日期时间。如果java中给定的日期时间有夏令时,我想根据输入考虑夏令时将其转换为美国时区。这是我试过的
function convert(Date dt,int toTimeZoneId){
Calendar cal = Calendar.getInstance();
cal.setTime(dt); // Geting time in IST
//Converted to GMT and set in cal
switch(toTimeZoneId){
case 1: tzTarget = TimeZone.getTimeZone("America/Adak");
offset = -10;
break;
case 2: tzTarget = TimeZone.getTimeZone("America/Anchorage");
offset = -9;
break;
case 3: tzTarget = TimeZone.getTimeZone("America/Los_Angeles");
offset = -8;
break;
case 4: tzTarget = TimeZone.getTimeZone("America/Denver");
offset = -7;
break;
case 5: tzTarget = TimeZone.getTimeZone("America/Chicago");
offset = -6;
break;
case 6: tzTarget = TimeZone.getTimeZone("America/New_York");
offset = -5;
break;
}
//converting from GMT to US timezones based on offset and dst
cal.setTimeZone(tzTarget);
dst = tzTarget.getDSTSavings();
dst = dst/3600000;
offset = offset + dst;
cal.add(Calendar.HOUR, offset);
Date date = cal.getTime();
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
}