標記之前重複仔細閱讀Plus Plus不能使用Joda Time和Java 8 time.util *。如何在沒有喬達時間的情況下在Java 7中正確處理夏令時?
我試圖讓DST啓用的國家的當前日期。因爲有時區,所以我得到[local-millis] = [UTC-millis] + [offset-millis]
,所以我加了DST_OFFSET
以在DST啓用的國家得到時間,就像在Linux機器GMT_TIME + LOCAL_TIME_ZONE_OFFSET + DST_OFFSET
上得到的那樣,以獲得當前的本地時間。
打印當前時間的代碼Istanbul當前DST已啓用1小時。
public class DstInstanbul {
public static void main(String...args){
Calendar calendar = Calendar.getInstance();
TimeZone timeZone = TimeZone.getTimeZone("Europe/Istanbul");
calendar.setTimeZone(timeZone);
calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
SimpleDateFormat simpleDateFormatLocal = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormatLocal.setTimeZone(TimeZone.getTimeZone("Europe/Istanbul"));
System.out.println("Current date and time : " + simpleDateFormatLocal.format(calendar.getTime()));
System.out.println("Current date and time : " + simpleDateFormatLocal.format(calendar.getTime()));
System.out.println("The TimeZone is : " + calendar.getTimeZone().getID());
}
}
這給了我正確的輸出
Current date and time : 2015-11-01 20:49:54
The Hour of the Day is : 20
The TimeZone is : Europe/Istanbul
但由於上面的代碼是沒有這麼多的通用的,所以我試圖添加以下行,因此,如果僅白天被啓用,則只會增加 的dstSaving所以我
if (timeZone.inDaylightTime(new Date())){
calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
}
改變後,calendar.add(Calendar.MILLISECOND, timeZone.getDSTSavings());
但問題是,如果我這樣做,我得到的輸出沒有任何DST 。並打印System.out.println(timeZone.inDaylightTime(new Date()));
給了我假,因此結果,但夏令時是有,你可以在這個鏈接看到Istanbul clock
Current date and time : 2015-11-01 19:54:49
The TimeZone is : Europe/Istanbul.
的時區Brazil同樣的邏輯也給了我真正的inDaylightTime,但顯示結果1小時現在領先
Ideone鏈接的方式訂購的所有代碼討論1. https://ideone.com/4NR5Ym 2. https://ideone.com/xH7vhp 3. https://ideone.com/tQenb5
我的問題是什麼是timeZone.inDaylightTime(new Date())
伊斯坦布爾添問題e區。爲什麼它顯示錯誤。爲什麼對巴西來說,即使inDaylightTime
爲真,我也沒有獲得當前的DST時間。 處理這種情況的正確方法是什麼?
確定了..但是對於巴西沒有這個calendar.add我得到正確的輸出意味着不需要添加dst你能建議爲什麼嗎? –
無法在Windows中完成!即使使用'GitBash',我也有例外:引起來自:java.io.EOFException at java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:268) ' – FaithReaper