Why the current year in the date saved as 3912?

前端 未结 2 1014
梦谈多话
梦谈多话 2021-01-11 19:25

to get the current date and time

final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calend         


        
相关标签:
2条回答
  • 2021-01-11 19:49

    Better to use Calendar

    Calendar cal=Calendar.getInstance();
    cal.set(Calendar.YEAR,myear);
    

    (i.e)

    cal.set(Calendar.YEAR,2016);
    
    0 讨论(0)
  • 2021-01-11 20:02

    From Date.setYear(int) description: Sets the gregorian calendar year since 1900 for this Date object. Thus, 1900 + 2012 = 3912.

    But calendar.get(Calendar.YEAR) returns exact year number 2012. So this inconsistency of API causes your issue. But anyway Date.setYear(int) is deprecated, thus, it is better to use Calendar object for date calculations.

    0 讨论(0)
提交回复
热议问题