Why does setting date with calendar give me the wrong date

前端 未结 3 1457
长发绾君心
长发绾君心 2021-01-24 07:20

I am setting the date to 2013-01-01 00:00:00, but the date comes out as Fri Feb 01 00:00:00 GMT+01:00 2013

Why?

Calendar calendar = Calendar.getInstance(         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-24 07:42

    Month numbering starts at 0 in Java's date classes. Use the month constants in the Calendar class to avoid this common mistake.

    calendar.set(2013, Calendar.JANUARY, 1, 0, 0, 0);
    

提交回复
热议问题