[Java] Date - 날짜 더하기 빼기

샘플코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public static void main(String[] args) {
	String currentTime = getCurrentDateString("yyyy-MM-dd'T'HH:mm:ssXXX");				
	try {
		String expireTime = AddDate(currentTime, 0, 0, 0, 2);
		Date currentDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").parse(currentTime);
		Date expireDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").parse(expireTime);
		
		long differenceSecond = (expireDate.getTime() - currentDate.getTime()) / 1000;
		System.out.println("currentDate : "+currentDate);
		System.out.println("expireDate : "+expireDate);
		System.out.println("differenceSecond : "+differenceSecond);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}

// 현재 시간을 주어진 포멧의 문자열로 반환 
public static String getCurrentDateString(String format){
	SimpleDateFormat formatSdf = new SimpleDateFormat(format);
	TimeZone time = TimeZone.getTimeZone("Asia/Seoul");
	formatSdf.setTimeZone(time);

	return formatSdf.format(new Date());
}

// 날짜 더하기 빼기
private static String AddDate(String strDate, int year, int month, int day, int minute) throws Exception {
	SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
	Calendar cal = Calendar.getInstance();
	Date dt = dtFormat.parse(strDate);
	cal.setTime(dt);
	cal.add(Calendar.YEAR, year);
	cal.add(Calendar.MONTH, month);
	cal.add(Calendar.DATE, day);
	cal.add(Calendar.MINUTE, minute);
	return dtFormat.format(cal.getTime());
}

Tags:

Updated:

Leave a comment