【问题标题】:java.util.Date shows different values with the same input parametersjava.util.Date 显示具有相同输入参数的不同值
【发布时间】:2012-03-05 23:44:34
【问题描述】:

我尝试 System.out 相同的字符串

System.out.println(" DATE 29 " + new Date(1330462800000l) + " Date 01 "
                + new Date(1330549200000l));

但是当我在构建中检查它(以控制台模式运行)和从 eclipse 运行应用程序时,我得到了不同的结果。

eclipse的输出(好像是正确的结果):

日期 2012 年 2 月 29 日星期三 00:00:00 EET 日期 01 星期四 3 月 1 日 00:00:00 EET 2012

构建输出(控制台模式)

日期 2012 年 2 月 28 日星期二 23:00:00 EET 日期 2 月 29 日星期三 23:00:00 EET 2012

很抱歉这个愚蠢的问题,但你对可能的原因有什么想法吗?

PS:我使用 maven+tycho 构建,打包类型的 eclipse-repository。(如果真的很重要)

编辑: 在 Eclipse 中,我查看了 timeZone 值:

Calendar calendar=Calendar.getInstance();
System.out.println("!!!time zone before: " +   calendar.getTimeZone());

!!!之前的时区: sun.util.calendar.ZoneInfo[id="Europe/Minsk",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=121,lastRule=java.util.SimpleTimeZone[id=Europe/Minsk,offset=7200000, dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=7200000,startTimeMode=1,endMode=2,endMonth=9,endDay=-1, endDayOfWeek=1,endTime=7200000,endTimeMode=1]]

然后我用硬编码的 zoneId 设置时区并进行构建

calendar.setTimeZone(TimeZone.getTimeZone("Europe/Minsk"));

没有任何结果

编辑:我在 build 和 eclipse 中使用了不同版本和架构的 jres。这可能是原因吗? 编辑:

System.out.println("!!!!!! system.timezone " + System.getProperty("user.timezone"));
System.setProperty("user.timezone", "Europe/Minsk");
System.out.println("!!!!!! system.timezone " + System.getProperty("user.timezone"));

!!!!!! system.timezone 欧洲/明斯克

!!!!!! system.timezone Europe/Minsk!!!区域后的日历时间: sun.util.calendar.ZoneInfo[id="Europe/Minsk",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=121,lastRule=java.util.SimpleTimeZone[id=Europe/Minsk,offset=7200000, dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=7200000,startTimeMode=1,endMode=2,endMonth=9,endDay=-1, endDayOfWeek=1,endTime=7200000,endTimeMode=1]]

正确的时区。但是日期还是错了

【问题讨论】:

  • VM 运行环境中的时区设置。
  • 抱歉,同一台机器可以不同吗?
  • 是的,TZ 是进程环境的一部分,可以更改。虽然看起来“EET”可能是 TZ,而且两者都是一样的,所以可能是一个红鲱鱼。
  • 您知道有一个历史视图可以查看对问题的编辑吗?您应该考虑将编辑内容合并到原始问题中以使其更具可读性

标签: date maven eclipse-plugin eclipse-rcp tycho


【解决方案1】:

加入测试:

System.out.println(Calendar.getInstance().getTimeZone());

System.getProperty("user.timezone");

Java时区可以通过系统属性user.timezone设置,详见Java system properties

时间使用joda time。您可以为所有 joda 对象重新定义时区:

DateTimeZone.setDefault(DateTimeZone.UTC);

【讨论】:

  • 添加了问题。至于 Joda 时间 - 然后我不能向项目添加新库:(
  • 所以试试时区。检查 System.getProperty("user.timezone")
【解决方案2】:

tl;博士

Instant.ofEpochMilli( 1_330_462_800_000L )     // Convert count-of-milliseconds-since-epoch to an `Instant`, a moment on the timeline in UTC.
       .atZone( ZoneId.of( "Europe/Minsk" ) )  // Adjust into another time zone. Same moment, different wall-clock time.

避免使用旧的日期时间类

您正在使用麻烦的旧日期时间类,这些类现在是遗留的,被 java.time 类所取代。在众多缺陷中,Date::toString 方法在生成字符串时动态应用了 JVM 当前的默认时区。 Date 代表 UTC 中的一个时刻,但这个善意的反功能会带来混乱。 JVM 当前的默认时区可以随时更改,即使在运行时,因此您可能会看到各种结果。

java.time

使用java.time.Instant 代替java.util.Date

Instant

Instant 类表示UTC 中时间轴上的时刻,分辨率为nanoseconds(最多九 (9) 位小数)。

Instant instant = Instant.ofEpochMilli( 1_330_462_800_000L ) ;

学习思考和主要在 UTC 工作。将 UTC 视为 One True Time™。所有其他时区只是该主题的变体。在作为程序员工作时,忘记您的个人当地时间,并停止在区域之间来回转换,因为这会让您发疯。在您的编程和日志记录中关注 UTC。

ZonedDateTime

要通过另一个时区的镜头查看同一时刻,请应用ZoneId 以获取ZonedDateTime 对象。我们仍然有相同的时刻,时间轴上的相同点,但有另一个挂钟时间。

ZoneId z = ZoneId.of( "Europe/Minsk" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

看到这个code run live at IdeOne.com

instant.toString(): 2012-02-28T21:00:00Z

zdt.toString(): 2012-02-29T00:00+03:00[欧洲/明斯克]


关于java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

从哪里获得 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多