Java 日历 'getTimeInMillis()' 导致所有 'isSet' 字段为真

标签 java calendar

我对 Java Calendar 的以下意外行为感到非常困惑

Calendar calendar = Calendar.getInstance();
calendar.clear();
//System.out(calendar.getTimeInMillis());
calendar.setLenient(false);
calendar.set(Calendar.YEAR, year);

如果我在“clear()”之后调用 getTimeInMillis(),所有在 clear() 之后为假的 isSet() 字段变为真。我究竟做错了什么?如果这是正确的行为(这看起来很奇怪),是否有办法阻止它?

最佳答案

tl;dr

永远不要使用糟糕的 Calendar 类。

使用现代 java.time 类。这些immutable objects完全避免您发现的问题。

ZonedDateTime                   // Represent a moment using the wall-clock time used by the people of a particular region (a time zone).
.now(                           // Capture the current moment.
    ZoneId.systemDefault()      // Get the JVM’s current default time zone. Beware: This can change at any moment during runtime. If crucial, confirm with user.
)                               // Returns a `ZonedDateTime` object.
.withYear(                      // Per the immutable objects pattern, produce another object based on the original’s values but with a change.
    2001                        // Change the year, but copy the month, day-of-month, hour, minute, second, fractional second, and time zone all the same. If the time-of-day is not valid on that date in the other year, auto-adjust per algorithm documented in the JavaDoc.
)                               // Returns another `ZonedDateTime` object.

java.time

可怕的 Calendar 类已经过时,多年前被现代 java.time 类取代。

要以 UTC 格式捕获当前时刻,请使用 Instant

Instant instant = Instant.now();

Instant 中,您可以获得与 Calendar 相同的纪元引用以来的毫秒数:1970 年的第一个时刻(UTC,1970-01-01T00:00) :00Z。谨防数据丢失,因为 Instant 的分辨率为纳秒级,比 Calendar 等传统日期时间类中使用的毫秒级要精细得多。

long millisecondsSinceEpoch = instant.toEpochMilli() ;

您可以根据该号码创建一个Instant

Instant instant = Instant.ofEpochMilli( millisecondsSinceEpoch) ;

要通过特定地区的人们使用的挂钟时间查看那个时刻,请应用时区 (ZoneId) 以获取 ZonedDateTime 对象。

ZoneId z = ZoneId.of( "America/Montreal" ) // Or your JVM’s current default, ZoneId.systemDefault().
ZonedDateTime zdt = instant.atZone( z ) ;

要像您在问题中所做的那样更改年份,请调用 withYear .

ZonedDateTime zdtOtherYear = zdt.withYear( 2001 ) ;

关于java.time

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

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

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

您可以直接与您的数据库交换java.time 对象。使用JDBC driver符合 JDBC 4.2或以后。不需要字符串,不需要 java.sql.* 类。

从哪里获得 java.time 类?

ThreeTen-Extra项目用附加类扩展 java.time。该项目是 future 可能添加到 java.time 的试验场。您可能会在这里找到一些有用的类,例如 Interval , YearWeek , YearQuarter , 和 more .

关于Java 日历 'getTimeInMillis()' 导致所有 'isSet' 字段为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53999525/

相关文章:

android - 最受支持的 Java 日期/时间类

java - Java中的TIntHashSet和Set之间是否有共同的父类?

Java 令人困惑的三元 Sonar 违规问题

java - 在基于 JSF 的 Web 应用程序中显示图像

java - 月份和 java.util.Formatter

java - af :Calendar component, 按列类型过滤

java - 将日期字符串从时区转换为不同时区

java - 如果java中没有指定修饰符会发生什么?

java - 如果手动完成,则不会调用 CompletableFuture 回调

python - 使用 Selenium 和 Python 的日历选择器