GregorianCalendar add, roll方法

 

细细体味 roll 和 add 的区别

 

直接上代码:

 

Using roll method

Case 1:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.FEBRUARY);
        cal.set(Calendar.DAY_OF_MONTH, 29);
        System.out.println(cal.getTime());                ==> Wed Feb 29 16:44:30 SGT 2012
        cal.roll(Calendar.DAY_OF_YEAR, true);
        System.out.println(cal.getTime());                ==> Thu Mar 01 16:44:30 SGT 2012

 

Case 2:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.FEBRUARY);
        cal.set(Calendar.DAY_OF_MONTH, 29);
        System.out.println(cal.getTime());                ==> Wed Feb 29 16:48:14 SGT 2012
        cal.roll(Calendar.DAY_OF_MONTH, true);
        System.out.println(cal.getTime());                ==> Wed Feb 01 16:48:14 SGT 2012

 

Case 3:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        System.out.println(cal.getTime());                ==> Mon Dec 31 16:50:12 SGT 2012
        cal.roll(Calendar.DAY_OF_MONTH, true);
        System.out.println(cal.getTime());                ==> Sat Dec 01 16:50:12 SGT 2012

 

Case 4:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        System.out.println(cal.getTime());                ==> Mon Dec 31 16:52:44 SGT 2012
        cal.roll(Calendar.DAY_OF_YEAR, true);
        System.out.println(cal.getTime());                ==> Sun Jan 01 16:52:44 SGT 2012

 

================================================================

Using add method

Case 1:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.FEBRUARY);
        cal.set(Calendar.DAY_OF_MONTH, 29);
        System.out.println(cal.getTime());                ==> Wed Feb 29 16:55:01 SGT 2012
        cal.add(Calendar.DAY_OF_YEAR, 1);
        System.out.println(cal.getTime());                ==> Thu Mar 01 16:55:01 SGT 2012

 

Case 2:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.FEBRUARY);
        cal.set(Calendar.DAY_OF_MONTH, 29);
        System.out.println(cal.getTime());                ==> Wed Feb 29 16:57:06 SGT 2012
        cal.add(Calendar.DAY_OF_MONTH, 1);
        System.out.println(cal.getTime());                ==> Thu Mar 01 16:57:06 SGT 2012

 

Case 3:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        System.out.println(cal.getTime());                ==> Mon Dec 31 16:59:12 SGT 2012
        cal.add(Calendar.DAY_OF_MONTH, 1);
        System.out.println(cal.getTime());                ==> Tue Jan 01 16:59:12 SGT 2013

 

Case 4:

        GregorianCalendar cal = new GregorianCalendar();
        cal.set(Calendar.YEAR, 2012);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 31);
        System.out.println(cal.getTime());                ==> Mon Dec 31 16:59:48 SGT 2012
        cal.add(Calendar.DAY_OF_YEAR, 1);
        System.out.println(cal.getTime());                ==> Tue Jan 01 16:59:48 SGT 2013

 

 

 

你可能感兴趣的:(calendar)