learning java Calendar类
//Calendar.MONTH ,这是一个特殊于日历的值。 //在格里高利历和罗马儒略历中一年中的第一个月是 JANUARY,它为 0;最后一个月取决于一年中的月份数。 // //所以这个值的初始值为0,所以我们用它来表示日历月份时需要加1 var c = Calendar.getInstance(); System.out.println(c.get(Calendar.DAY_OF_WEEK)); System.out.println(c.get(Calendar.YEAR)); System.out.println(c.get(Calendar.MONTH)); System.out.println(c.get(Calendar.DATE)); System.out.println(c.get(Calendar.HOUR)); System.out.println(c.get(Calendar.MINUTE)); System.out.println(c.get(Calendar.SECOND)); c.set(2003,10,23,12,32,23); System.out.println(c.get(Calendar.YEAR)); System.out.println(c.get(Calendar.MONTH)); System.out.println(c.get(Calendar.DATE)); System.out.println(c.get(Calendar.HOUR)); System.out.println(c.get(Calendar.MINUTE)); System.out.println(c.get(Calendar.SECOND)); // add 会进位 c.add(Calendar.YEAR, -1); System.out.println(c.getTime()); // roll 当修改的字段超出它的允许范围时,上一级字段不会增大。 c.roll(Calendar.MONTH, -11); System.out.println(c.getTime());
output:
6 2019 6 26 8 54 16 2003 10 23 0 32 23 Sat Nov 23 12:32:23 CST 2002 Mon Dec 23 12:32:23 CST 2002