我正在尝试获取当地时间,当时是在不同时区的午夜。 我的意思是:
这是我的代码
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
我期待:
London: 2013-04-13 01:00:00.000
Helsinki: 2013-04-12 23:00:00.000
对赫尔辛基而言,结果与预期一致,但伦敦是一个疯狂的结果,我不明白它来自何处。
-----编辑-----
具有预期输出的最终代码:
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.
答案 0 :(得分:3)
尝试将小时更改为HOUR_OF_DAY。它似乎是读取时间一次为AM,一次为PM。
更多信息: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html
答案 1 :(得分:2)
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
答案 2 :(得分:1)
您正在使用stockolm时区解析日期:
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的小时。
*编辑*
在伦敦,斯德哥尔摩和赫尔辛基获得时间:
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