java - 为什么 GregorianCalendar.getTimeInMillis() 和 GregorianCalendargetTimeInMillis(1970.01.01) 之间存在差异

标签 java calendar

我尝试将出生日期和当前日期进行比较 然后我试图了解年龄。 但当前日期函数以给定日期之外的另一个日期开始 我的代码:

GregorianCalendar birthdate = new GregorianCalendar(2001,06,20);
long ms = System.currentTimeMillis() - birthdate.getTimeInMillis();
double years = (((double)ms / 1000)/31536000) ;
System.out.print(years);
// years Shoud be 18 here but it returns 17.3
if (years > 18) {
  // Code Block
}
else{
  System.out.print("to Jung");
}

最佳答案

检查Constructor on GregorianCalendar.

public GregorianCalendar(int year, int month, int dayOfMonth)

Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.

year - the value used to set the YEAR calendar field in the calendar.

month - the value used to set the MONTH calendar field in the calendar. Month value is 0-based. e.g., 0 for January.

dayOfMonth - the value used to set the DAY_OF_MONTH calendar field in the calendar.

您正在创建 7 月 20 日的日期,该日期恰好偏移了一个月,即大约 0.08 年。

关于java - 为什么 GregorianCalendar.getTimeInMillis() 和 GregorianCalendargetTimeInMillis(1970.01.01) 之间存在差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56689941/

相关文章:

javascript - 时间戳到文本

java - 如何从日历中获取昨天的实例?

ios - 有没有办法向 iOS 应用程序添加日历 View ?

java - 由 : java. lang.IllegalStateException : package not installed? 引起

Java,不用服务器连接mysql文件

java - 将 2 个日期与一种格式结合起来?

java - 如何通过编程代码到达日历中的编辑事件?

java - 在java中使用REST API创建一个新的 "JIRA issue"

java - 带有 nextLine 条件的 While 循环

java - 在 Windows 7 平板电脑中执行时,我的 GUI 看起来很奇怪