| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 
 | import java.util.Calendar;
import java.util.TimeZone;
 
public class Test {
public static void main(String[] args) throws Exception {
 
// Construct FROM and TO TimeZone instances
TimeZone fromTimeZone = TimeZone.getTimeZone(America/Chicago);
TimeZone toTimeZone = TimeZone.getDefault();
 
// Get a Calendar instance using the default time zone and locale.
Calendar calendar = Calendar.getInstance();
 
// Set the calendars time with the given date
calendar.setTimeZone(fromTimeZone);
 
// calendar.setTime(new Date());
calendar.set(2011, 4  1, 12  1); // APR 12 2011
calendar.set(Calendar.HOUR, 12  1); // 11
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
 
calendar.set(Calendar.AM_PM, Calendar.PM);
System.out.println(Input :  + calendar.getTime() +  in 
+ fromTimeZone.getDisplayName());
 
// FROM TimeZone to UTC
int rawOffset_from_UTC = fromTimeZone.getRawOffset();
calendar.add(Calendar.MILLISECOND, rawOffset_from_UTC * -1);
 
//System.out.println(rawOffset_from_UTC);
if (fromTimeZone.inDaylightTime(calendar.getTime())) {
int dstOffset_from_UTC = calendar.getTimeZone().getDSTSavings();
//System.out.println(dstOffset_from_UTC);
calendar.add(Calendar.MILLISECOND, dstOffset_from_UTC * -1);
}
// UTC to TO TimeZone
calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset());
 
if (toTimeZone.inDaylightTime(calendar.getTime())) {
calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings());
}
System.out.println(Output:  + calendar.getTime() +  in 
+ toTimeZone.getDisplayName());
}
} | 
Partager