本上,您正在做的轉換相關的日期。
您只指定小時和分鐘,因此計算是在1970年1月1日完成的。在那個日期,大概是您的時區的GMT偏移量只有2小時。
也指定日期。
SimpleDateFormat inputFormat =
new SimpleDateFormat("kk:mm", Locale.US);
inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat outputFormat =
new SimpleDateFormat("yyyy/MM/dd kk:mm", Locale.US);
outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = inputFormat.parse("12:00");
System.out.println("Time Is: " + outputFormat.format(date));
Ideone demo
輸出:
Time Is: 1970/01/01 12:00
附加代碼來顯示夏令時/夏時制的影響:
SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy/MM/dd kk:mm", Locale.US);
gmtFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
SimpleDateFormat finlandFormat = new SimpleDateFormat("yyyy/MM/dd kk:mm zzz", Locale.US);
finlandFormat.setTimeZone(TimeZone.getTimeZone("Europe/Helsinki"));
SimpleDateFormat plus3Format = new SimpleDateFormat("yyyy/MM/dd kk:mm zzz", Locale.US);
plus3Format.setTimeZone(TimeZone.getTimeZone("GMT+3"));
Date date = gmtFormat.parse("1970/01/01 12:00");
System.out.println("Time Is: " + gmtFormat.format(date));
System.out.println("Time Is: " + finlandFormat.format(date));
System.out.println("Time Is: " + plus3Format.format(date));
date = gmtFormat.parse("2016/04/22 12:00");
System.out.println("Time Is: " + gmtFormat.format(date));
System.out.println("Time Is: " + finlandFormat.format(date));
System.out.println("Time Is: " + plus3Format.format(date));
輸出:
Time Is: 1970/01/01 12:00
Time Is: 1970/01/01 14:00 EET <-- Eastern European Time
Time Is: 1970/01/01 15:00 GMT+03:00
Time Is: 2016/04/22 12:00
Time Is: 2016/04/22 15:00 EEST <-- Eastern European Summer Time
Time Is: 2016/04/22 15:00 GMT+03:00
不是我,輸出爲:1970/01/01 14:00,我不認爲這是關係到一年,因爲它需要的GMT從設備偏移,那麼,爲什麼它還需要年嗎? 。 – Jaeger
我敢打賭,你不知道在Unix時代(1970/01/01),歐洲/倫敦的GMT抵消時間實際上是1小時,因爲那個時候英國在1968年10月27日到1971年10月31日之間是永久夏令時](https://en.wikipedia.org/wiki/British_Summer_Time#Periods_of_deviation)。在yyyy/01/01之後,每年都是+ 0h。不知道這一年,你不能正確地轉換,因爲這樣的原因。 –
另外,夏令時不會在每年的同一天開始和結束。 –