我试图得到我的当地时间,当它是午夜在不同的时区。我的意思是:
- 斯德哥尔摩现在几点,伦敦现在是午夜?
- 斯德哥尔摩是几点,赫尔辛基是午夜?
下面是我的代码
public static void main(String[] args) throws ParseException {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
Date date = formatter.parse("2013-04-13 00:00:00.000");
System.out.println("London: " + formatter.format(getFirstInstantOfDay("Europe/London", date)));
System.out.println("Helsinki: " + formatter.format(getFirstInstantOfDay("Europe/Helsinki", date)));
}
public static Date getFirstInstantOfDay(String timeZoneId, Date date) {
Calendar resultDate = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId));
resultDate.setTime(date);
resultDate.set(Calendar.HOUR, 0);
resultDate.set(Calendar.MINUTE, 0);
resultDate.set(Calendar.SECOND, 0);
resultDate.set(Calendar.MILLISECOND, 0);
return resultDate.getTime();
}
输出为:
London: 2013-04-12 13:00:00.000
Helsinki: 2013-04-12 23:00:00.000
And I expected:
London: 2013-04-13 01:00:00.000
Helsinki: 2013-04-12 23:00:00.000
赫尔辛基的结果和预期的一样,但伦敦的结果很疯狂,我不知道它从何而来。
----- EDIT -----
期望输出的最终代码:
String dateStr = "2013-04-13";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date dateGB = formatter.parse(dateStr);
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
Date dateSE = formatter.parse(dateStr);
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Helsinki"));
Date dateFI = formatter.parse(dateStr);
DateFormat stockholmFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
stockholmFormatter.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
System.out.println("Midnight in London is " + stockholmFormatter.format(dateGB) + " in Stockholm.");
System.out.println("Midnight in Stockholm is " + stockholmFormatter.format(dateSE) + " in Stockholm.");
System.out.println("Midnight in Helsinki is " + stockholmFormatter.format(dateFI) + " in Stockholm.");
输出:Midnight in London is 2013-04-13 01:00:00.000 in Stockholm.
Midnight in Stockholm is 2013-04-13 00:00:00.000 in Stockholm.
Midnight in Helsinki is 2013-04-12 23:00:00.000 in Stockholm.
尝试将Hour更改为HOUR_OF_DAY。它似乎一次读取时间为上午,一次读取时间为下午。
更多信息:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
Calender
有两种方式设置一天中的小时。
HOUR_OF_DAY
:
resultDate.set(Calendar.HOUR_OF_DAY, 0);
第二个选项是通过'HOUR'和'AM_PM'
resultDate.set(Calendar.HOUR, 0);
resultDate.set(Calendar.AM_PM,Calendar.AM);
编辑:你得到的2013-04-12是正确的。
您在斯德哥尔摩输入2013-04-13 00:00:00.000
与伦敦时间2013-04-12 23:00:00.000相同
然后将小时重置为00,在伦敦产生2013-04-12 00:00:00.000
然后回到斯德哥尔摩时间产生
2013-04-12 01:00:00.000
当你做测试(在2013-04-13 00:00:00.00在斯德哥尔摩)伦敦仍在前一天(2013-04-12)。
编辑
你可能想知道的是不同城市的午夜是什么时候(用斯德哥尔摩时间表示)。我会这样写:
public static void main(String[] args) throws ParseException {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
String date = "2013-04-13";
System.out.println("Stockholm: " + formatter.format(getMidnightTime("Europe/Stockholm", date)));
System.out.println("London: " + formatter.format(getMidnightTime("Europe/London", date)));
System.out.println("Helsinki: " + formatter.format(getMidnightTime("Europe/Helsinki", date)));
}
public static Date getMidnightTime(String timeZoneId, String date) throws ParseException {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setTimeZone(TimeZone.getTimeZone(timeZoneId));
return formatter.parse(date);
}
Stockholm: 2013-04-13 00:00:00.000
London: 2013-04-13 01:00:00.000
Helsinki: 2013-04-12 23:00:00.000
您正在用斯德哥尔摩时区解析日期:
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
和您解析为伦敦时区和赫尔辛基的结果日期:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date date = formatter.parse("2013-04-13 00:00:00.000");
System.out.println("London: " + formatter.format(getFirstInstantOfDay("Europe/London", date)));
System.out.println("Helsinki: " + formatter.format(getFirstInstantOfDay("Europe/Helsinki", date)));
的结果是:
London: 2013-04-13 00:00:00.000
Helsinki: 2013-04-12 22:00:00.000
请注意,您的格式有HH,表示从0到23的小时。
* edit *
查询伦敦、斯德哥尔摩和赫尔辛基的时间:
formatter.setTimeZone(TimeZone.getTimeZone("Europe/London"));
Date date = formatter.parse("2013-04-13 00:00:00.000");
System.out.println("London: " + formatter.format(date));
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Stockholm"));
System.out.println("Stockolm: " + formatter.format(date));
formatter.setTimeZone(TimeZone.getTimeZone("Europe/Helsinki"));
System.out.println("Helsinki: " + formatter.format(date));
输出为:
London: 2013-04-13 00:00:00.000
Stockolm: 2013-04-13 01:00:00.000
Helsinki: 2013-04-13 02:00:00.000