Java

java中Calendar类,任意一天加一个月后的日期测试

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期测试
 * @author			石马人山  liuyuewei@longlonggo.com
 * 
 */
public class DateTest {
    
	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d;
		Calendar cal;
		
		try {
			//平年-->1月每天加一个月
			cal = Calendar.getInstance();
			for (int i = 1; i <= 31; i++) {
				d = sdf.parse("2017-01-"+i);
				cal.setTime(d);
				cal.add(Calendar.MONTH, 1);
				System.out.println(sdf.format(d) + ",加一月:" + sdf.format(cal.getTime()));
			}
			System.out.println();
			System.out.println();
			
			//平年-->4月每天加一个月
			cal = Calendar.getInstance();
			for (int i = 1; i < 31; i++) {
				d = sdf.parse("2017-04-"+i);
				cal.setTime(d);
				cal.add(Calendar.MONTH, 1);
				System.out.println(sdf.format(d) + ",加一月:" + sdf.format(cal.getTime()));
			}
			System.out.println();
			System.out.println();

			//闰年-->1月每天加一个月
			cal = Calendar.getInstance();
			for (int i = 1; i <= 31; i++) {
				d = sdf.parse("2016-01-"+i);
				cal.setTime(d);
				cal.add(Calendar.MONTH, 1);
				System.out.println(sdf.format(d) + ",加一月:" + sdf.format(cal.getTime()));
			}
			System.out.println();
			System.out.println();
			
			//闰年-->4月每天加一个月
			cal = Calendar.getInstance();
			for (int i = 1; i < 31; i++) {
				d = sdf.parse("2017-04-"+i);
				cal.setTime(d);
				cal.add(Calendar.MONTH, 1);
				System.out.println(sdf.format(d) + ",加一月:" + sdf.format(cal.getTime()));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
平年–>1月每天加一个月 平年–>4月每天加一个月 闰年–>1月每天加一个月 闰年–>4月每天加一个月

2017-01-01,加一月:2017-02-01

2017-01-02,加一月:2017-02-02

2017-01-03,加一月:2017-02-03

2017-01-04,加一月:2017-02-04

2017-01-05,加一月:2017-02-05

2017-01-06,加一月:2017-02-06

2017-01-07,加一月:2017-02-07

2017-01-08,加一月:2017-02-08

2017-01-09,加一月:2017-02-09

2017-01-10,加一月:2017-02-10

2017-01-11,加一月:2017-02-11

2017-01-12,加一月:2017-02-12

2017-01-13,加一月:2017-02-13

2017-01-14,加一月:2017-02-14

2017-01-15,加一月:2017-02-15

2017-01-16,加一月:2017-02-16

2017-01-17,加一月:2017-02-17

2017-01-18,加一月:2017-02-18

2017-01-19,加一月:2017-02-19

2017-01-20,加一月:2017-02-20

2017-01-21,加一月:2017-02-21

2017-01-22,加一月:2017-02-22

2017-01-23,加一月:2017-02-23

2017-01-24,加一月:2017-02-24

2017-01-25,加一月:2017-02-25

2017-01-26,加一月:2017-02-26

2017-01-27,加一月:2017-02-27

2017-01-28,加一月:2017-02-28

2017-01-29,加一月:2017-02-28

2017-01-30,加一月:2017-02-28

2017-01-31,加一月:2017-02-28

2017-04-01,加一月:2017-05-01

2017-04-02,加一月:2017-05-02

2017-04-03,加一月:2017-05-03

2017-04-04,加一月:2017-05-04

2017-04-05,加一月:2017-05-05

2017-04-06,加一月:2017-05-06

2017-04-07,加一月:2017-05-07

2017-04-08,加一月:2017-05-08

2017-04-09,加一月:2017-05-09

2017-04-10,加一月:2017-05-10

2017-04-11,加一月:2017-05-11

2017-04-12,加一月:2017-05-12

2017-04-13,加一月:2017-05-13

2017-04-14,加一月:2017-05-14

2017-04-15,加一月:2017-05-15

2017-04-16,加一月:2017-05-16

2017-04-17,加一月:2017-05-17

2017-04-18,加一月:2017-05-18

2017-04-19,加一月:2017-05-19

2017-04-20,加一月:2017-05-20

2017-04-21,加一月:2017-05-21

2017-04-22,加一月:2017-05-22

2017-04-23,加一月:2017-05-23

2017-04-24,加一月:2017-05-24

2017-04-25,加一月:2017-05-25

2017-04-26,加一月:2017-05-26

2017-04-27,加一月:2017-05-27

2017-04-28,加一月:2017-05-28

2017-04-29,加一月:2017-05-29

2017-04-30,加一月:2017-05-30

2016-01-01,加一月:2016-02-01

2016-01-02,加一月:2016-02-02

2016-01-03,加一月:2016-02-03

2016-01-04,加一月:2016-02-04

2016-01-05,加一月:2016-02-05

2016-01-06,加一月:2016-02-06

2016-01-07,加一月:2016-02-07

2016-01-08,加一月:2016-02-08

2016-01-09,加一月:2016-02-09

2016-01-10,加一月:2016-02-10

2016-01-11,加一月:2016-02-11

2016-01-12,加一月:2016-02-12

2016-01-13,加一月:2016-02-13

2016-01-14,加一月:2016-02-14

2016-01-15,加一月:2016-02-15

2016-01-16,加一月:2016-02-16

2016-01-17,加一月:2016-02-17

2016-01-18,加一月:2016-02-18

2016-01-19,加一月:2016-02-19

2016-01-20,加一月:2016-02-20

2016-01-21,加一月:2016-02-21

2016-01-22,加一月:2016-02-22

2016-01-23,加一月:2016-02-23

2016-01-24,加一月:2016-02-24

2016-01-25,加一月:2016-02-25

2016-01-26,加一月:2016-02-26

2016-01-27,加一月:2016-02-27

2016-01-28,加一月:2016-02-28

2016-01-29,加一月:2016-02-29

2016-01-30,加一月:2016-02-29

2016-01-31,加一月:2016-02-29

2016-04-01,加一月:2016-05-01

2016-04-02,加一月:2016-05-02

2016-04-03,加一月:2016-05-03

2016-04-04,加一月:2016-05-04

2016-04-05,加一月:2016-05-05

2016-04-06,加一月:2016-05-06

2016-04-07,加一月:2016-05-07

2016-04-08,加一月:2016-05-08

2016-04-09,加一月:2016-05-09

2016-04-10,加一月:2016-05-10

2016-04-11,加一月:2016-05-11

2016-04-12,加一月:2016-05-12

2016-04-13,加一月:2016-05-13

2016-04-14,加一月:2016-05-14

2016-04-15,加一月:2016-05-15

2016-04-16,加一月:2016-05-16

2016-04-17,加一月:2016-05-17

2016-04-18,加一月:2016-05-18

2016-04-19,加一月:2016-05-19

2016-04-20,加一月:2016-05-20

2016-04-21,加一月:2016-05-21

2016-04-22,加一月:2016-05-22

2016-04-23,加一月:2016-05-23

2016-04-24,加一月:2016-05-24

2016-04-25,加一月:2016-05-25

2016-04-26,加一月:2016-05-26

2016-04-27,加一月:2016-05-27

2016-04-28,加一月:2016-05-28

2016-04-29,加一月:2016-05-29

2016-04-30,加一月:2016-05-30

//结论:月份和年份的相加,如果当前日期的日期数,大于相加后的那个月的总天数,则取值相加后结果月的最后一天

留言

您的电子邮箱地址不会被公开。 必填项已用*标注