android – 为什么今年的日期保存为3912?

前端之家收集整理的这篇文章主要介绍了android – 为什么今年的日期保存为3912?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
获取当前的日期和时间
  1. final Calendar c = Calendar.getInstance();
  2. mYear = c.get(Calendar.YEAR);
  3. mMonth = c.get(Calendar.MONTH);
  4. mDay = c.get(Calendar.DAY_OF_MONTH);
  5. mHour = c.get(Calendar.HOUR_OF_DAY);
  6. mMinute = c.get(Calendar.MINUTE);

创建当前日期对象

  1. Date toDate;
  2. toDate.setYear(mYear);
  3. toDate.setMonth(mMonth);
  4. toDate.setDate(mDay);
  5.  
  6.  
  7.  
  8. Date endDate = toDate;

当打印endDate对象时,我得到了

  1. Mon Jan 01 13:11:00 GMT+03:00 3912

为什么

解决方法

From Date.setYear(int)description:设置自1900年以来的Date对象的gregorian日历年.因此,1900年2012 = 3912.

但是calendar.get(Calendar.YEAR)会返回确切的年份.因此,API的这种不一致会导致您的问题.但是无论如何,Date.setYear(int)已被弃用,因此,最好使用Calendar对象进行日期计算.

猜你在找的Android相关文章