본문 바로가기

개발/Java

[JAVA/자바] 년,월,일 날짜 더하기

년,월,일 날짜 더하기

특정 날짜에서부터  더하거나 빼진  날짜를 가져오고자 할때 사용한다.


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

public class DateUtil {

	public static void main(String[] args) throws Exception  {
		String date = "20180910";
		String result = addDate(date,1,12,1);
		System.out.println(result);
		
	}


    /**
     * 년 월 일 날짜 더하기
     * 
     * @param dt(날짜) , y(년) , m(월), d(일)
     * @Exam  addDate("20180910",1,12,1) -->20200911
     * @return String
     */
	private static String addDate(String dt, int y, int m, int d) throws Exception  {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

		Calendar cal = Calendar.getInstance();
		Date date = format.parse(dt);
		cal.setTime(date);
        cal.add(Calendar.YEAR, y);		//년 더하기
        cal.add(Calendar.MONTH, m);		//년 더하기
        cal.add(Calendar.DATE, d);		//년 더하기

		return format.format(cal.getTime());

	}

}