我想將當前日期轉換爲美國/蒙特利爾時區。我這樣做:Java將日期轉換爲EST時區以尊重DST
Date date = new Date();
TimeZone timeZone = TimeZone.getTimeZone ("America/Montreal");
Calendar cal = new GregorianCalendar(timeZone);
cal.setTime(date);
String whatIWant = "" + cal.get(Calendar.HOUR_OF_DAY) + ':'+
cal.get(Calendar.MINUTE)+ ':'+ cal.get(Calendar.SECOND);
log.info(whatIWant);
轉換是很好,但我想知道這個代碼是多麼強大。在沒有夏令時會發生什麼?
你說*忽略*使用已過時的API,但它們在這裏顯然是相關的,因爲'Date'構造函數使用當前時區。完全不清楚問題是什麼。另外,爲什麼你不使用SimpleDateFormat? –