8


返回的时间和日期是正确的,但小时除外,它比应​​有的时间少 1 小时。

我似乎正在设置获得正确时间和日期所需的一切:

 - I'm using Calendar.getInstance(), instead of new Date()
 - I'm setting the timezone of the Calendar instance with Timezone.getTimeZone
 - I'm using DateFormat and SimpleDateFormat to format the output


我的时区是Eastern Standard Timeaka UTC/GMT-5:00这些行都没有任何效果:

 - cal.setTimeZone(TimeZone.getTimeZone(cal.getTimeZone().getDisplayName()));
 - cal.setTimeZone(TimeZone.getTimeZone("EST"));
 - cal.setTimeZone(TimeZone.getTimeZone("UTC"));
 - cal.setTimeZone(TimeZone.getTimeZone("GMT"));
 - cal.setTimeZone(TimeZone.getTimeZone("GMT-5:00"));

...但是这些选项中的每一个都设置了我想要的时区。


这是我的尝试,我错误地向Calendar实例添加了 1 小时:

PrintWriter output = null;

try {
  output = new PrintWriter(
   new BufferedWriter(new FileWriter("output.txt", true)));

  DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:ms MM/dd/yyyy");
  Calendar cal = Calendar.getInstance();

  // ...doesn't seem to be working:
  cal.setTimeZone(TimeZone.getTimeZone(cal.getTimeZone().getDisplayName()));

  /*
   * adding an extra hour here, to make up for the incorrect hour value???
   * ...without this line, everything is correct except the hour = n - 1:
   */
  //cal.setTimeInMillis(cal.getTimeInMillis() + (1000 * 60 * 60));

  // printing to console here:
  System.out.println(dateFormat.format(cal.getTime()));
  System.out.println(cal.getTimeZone().getDisplayName());

  // printing to the log-file here:
  output.println(dateFormat.format(cal.getTime()));
  output.println(cal.getTimeZone().getDisplayName());

} catch (IOException e) {
  e.printStackTrace();

} finally {
  if (output != null) {
    output.close();
  }
}


输出:

10:05:43:543 10/10/2013
GMT-05:00

错误——它应该是11:05:43:543!( ps - 不幸的是我不能使用Joda-Time

4

3 回答 3

3

1) 在 SimpleDateFormat 中设置 TimeZone

2) Either use UTC, or a "real" Time Zone like ("Austria/Vienna");
(Country name, and biggest city in TimeZone look it up to be sure)
EST (=UTC-5) is not a time zone very suitable for computational purpose, because it is the time without daylight saving.

One more example from central europe:
In Winter we use MEZ (CET), in Summer (Daylight savings) we use (MESZ = CEST).
But you want that your computer caluclates that for you, so don't use that:

TimeZones are geo poilitical, therfore the name of the country is needed.
Each country can decide to change its time zone when they want (e.g russia some time ago, and spain is discussing now.)

于 2013-10-10T16:17:39.300 回答
1

日历不考虑夏令时。

根据这篇文章:如何在 java 中使用 Timezone 解决夏令时

应完全避免使用 3 个字母的缩写,以支持 TZDB 区域 ID。EST 是东部标准时间 - 标准时间从不遵守 DST;这不是一个完整的时区名称。它是用于部分时区的名称。(不幸的是,我还没有找到一个关于“半时区”概念的好词。)

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"));

这是一个很好的起点:http: //download.java.net/jdk8/docs/api/java/time/ZoneId.html

于 2013-10-10T15:14:29.360 回答
0


For posterity, here is my improved code based on the post "How to tackle daylight savings using Timezone in java":

PrintWriter output = null;

try {
  output = new PrintWriter(
        new BufferedWriter(new FileWriter("output.txt", true)));

  // here are the relevant changes (a lot less code!):
  TimeZone zone = TimeZone.getTimeZone("America/New_York");
  SimpleDateFormat simpleFormat = new SimpleDateFormat("HH:mm:ss:ms MM/dd/yyyy");
  simpleFormat.setTimeZone(zone);

  // printing to console here:
  System.out.println(simpleFormat.format(new Date()));
  System.out.println(simpleFormat.getTimeZone().getDisplayName());

  // printing to the log-file here:
  output.println(simpleFormat.format(new Date()));
  output.println(simpleFormat.getTimeZone().getDisplayName());

} catch (IOException e) {
  e.printStackTrace();

} finally {
  if (output != null) {
    output.close();
  }
}


Note that I'm using SimpleDateFormat instead of DateFormat, to customize the output format (don't need to use both).

于 2013-10-15T14:12:32.020 回答