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