자바에서 java.util의 Calendar와 Date를 이용하여 날짜 계산하는 법을 알아보자
1. 현재 시간에 날짜 더하기, 빼기
Calendar cal1 = Calendar.getInstance(); cal1.add(Calendar.DATE, 6); // 일 계산 cal1.add(Calendar.MONTH, 4); // 월 연산 cal1.add(Calendar.DATE, -3); // 빼고 싶다면 음수 입력 Date date = new Date(cal1.getTimeInMillis()); System.out.println("현재시간 : " + new Date()); System.out.println("연산시간 : " + date);결과
2. 특정 시간에 연산
String date_str = "2021-03-01 11:11:11"; SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = transFormat.parse(date_str); Calendar cal1 = Calendar.getInstance(); cal1.setTime(date); // 시간 설정 cal1.add(Calendar.YEAR, 2); // 년 연산 cal1.add(Calendar.MONTH, 4); // 월 연산 cal1.add(Calendar.DATE, 4); // 일 연산 cal1.add(Calendar.HOUR_OF_DAY , 4); // 시간 연산 cal1.add(Calendar.MINUTE, 5); // 분 연산 cal1.add(Calendar.SECOND, 12); // 초 연산 System.out.println("설정시간 : " + date); System.out.println("연산시간 : " + new Date(cal1.getTimeInMillis()));결과
3. 서로 다른 두개 시간 연산
String date_str1 = "2021-01-01 11:11:11"; String date_str2 = "2000-03-01 01:12:12"; SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = transFormat.parse(date_str1); Date date2 = transFormat.parse(date_str2); Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); // 시간 설정 cal1.setTime(date1); cal2.setTime(date2); // 날짜 연산 cal1.add(Calendar.MONTH , cal2.get(Calendar.MONTH) + 1); // 월 더하기 - get(Calendar.MONTH)의 결과값이 0~11 이기 때문에 1을 더한다 cal1.add(Calendar.DATE, cal2.get(Calendar.DATE)); // 일 더하기 cal1.add(Calendar.HOUR_OF_DAY, cal2.get(Calendar.HOUR_OF_DAY)); // 시간 더하기 System.out.println("date1 : " + date1); System.out.println("date2 : " + date2); System.out.println("연산시간 : " + new Date(cal1.getTimeInMillis()));결과
반응형
SimpleDateFormat, Calendar 클래스를 이용하여 특정 날짜에서 더하거나 뺀 날짜를 구할 수 있다.
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateAdd { public static void main(String[] args) throws Exception{ String date = "20200801"; //1년 후 날짜 String addYear = AddDate(date, 1, 0, 0); //1달 후 날짜 String addMonth = AddDate(date, 0, 1, 0); //1일 후 날짜 String addDay = AddDate(date, 0, 0, 1); System.out.println(addYear); //20210801 System.out.println(addMonth); //20200901 System.out.println(addDay); //20200802 } private static String AddDate(String strDate, int year, int month, int day) throws Exception { SimpleDateFormat dtFormat = new SimpleDateFormat("yyyyMMdd"); 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); return dtFormat.format(cal.getTime()); } }
위의 예제와 같이 Calendar 객체의 add() 메서드를 사용하면 원하는 날짜를 계산할 수 있다.
만약 특정일 기준으로 원하는 날짜만큼 빼려면 원하는 만큼의 년, 월, 일에 -(마이너스)를 붙여 add 메서드 파라미터에 세팅한다.
//year 년 전 cal.add(Calendar.YEAR, -year); //month 월 전 cal.add(Calendar.MONTH, -month); //month 일 전 cal.add(Calendar.DATE, -day);
어제 날짜 구하기(일 년 전, 한 달 전)
//위의 메소드에 적용하면 다음과 같이 파라메터를 세팅하면된다. //일년 전 AddDate(date, -1, 0, 0); //한달 전 AddDate(date, 0, -1, 0); //하루 전 AddDate(date, 0, 0, -1);
공유하기
게시글 관리
구독하기코딩 시그널저작자표시
'개발언어 > JAVA' 카테고리의 다른 글
[JAVA] 자바 날짜 포맷 변경 방법(SimpleDateFormat) yyyyMMdd (1)2020.08.06[JAVA] 람다식(Lambda Expressions)이란? 사용방법 & 장단점 (0)2020.08.04[JAVA] 제네릭(Generic)이란? 제네릭 사용 방법과 예제 정리 (0)2020.08.01[JAVA] ArrayList를 이용하여 Stack, Queue 구현하기 (1)2020.03.17[JAVA]자바 변수란 무엇인가? (0)2020.03.12����, ���Ϻһ��Կ�Ģ�� ������� �ʰ� ���Ϻ��� ���ԵǴ� ��쿡��, ���ϻ��Թ�ư�� ��� �Է��Ͻø� �˴ϴ�. (���� 0�ú��� �����ϴ� ���, ���ɰ��, ���ڰ�� ��)
��. ������/������ �Է� �� �������
����ϰ��� �ϴ� �����ϰ� �������� �Է� �� ������⸦ Ŭ���ϸ� ������� ���õ˴ϴ�.
������� �����ϰ� ó������ ���ư����� ���� ��ư�� Ŭ���ϸ� �˴ϴ�.
��. ���ȭ��
��������� �� �Է��� ��¥�� �������� ��, ��, ��, ������ ȯ���Ͽ� �� ������� �����մϴ�.
��, ��, ���� �ι� ������ ���� ������ ������ ����˴ϴ�.
�� ���� : �Ⱓ�� ������ ȯ��
��. ���ϻ���/�һ��� ����
�Ⱓ �������� ��꿡 ���� �������� �����ϴ� ���Դϴ�. ��κ��� ��� ���� �һ����� ��Ģ�� ����˴ϴ�.
����, ���Ϻһ��Կ�Ģ�� ������� �ʰ� ���Ϻ��� ���ԵǴ� ��쿡��, ���ϻ��Թ�ư�� ��� �Է��Ͻø� �˴ϴ�. (���� 0�ú��� �����ϴ� ���, ���ɰ��, ���ڰ�� ��)
��. �Ⱓ �Է� �� �������
���� �Ⱓ���� ��, ��, ��, �� ���� �����Ͽ� �Է��� ��, �Ⱓ �������� �Է��մϴ�.
����(��ߩ) ��ư�� Ŭ���� �巡�� ���Ͽ� �� �������� ǥ�õǸ�, ����(�ߩ)��ư Ŭ���� ���ŷ� �ұ� ��¥�� ǥ�õ˴ϴ�.
������� �����ϰ� ó������ ���ư����� ���� ��ư�� Ŭ���ϸ� �˴ϴ�.
�� ���ñ��� : �ι� ��6�� �Ⱓ
��155��(������ �������)
�Ⱓ�� ����� ����, ���ǻ��� ó�� �Ǵ� ���������� �ٸ� ���� �ٰ� ������ ������ ������ ���Ѵ�.
��156��(�Ⱓ�� �����)
�Ⱓ�� ��, ��, �ʷ� ���� ������ ��÷κ��� ����Ѵ�.
��157��(�Ⱓ�� �����)
�Ⱓ�� ��, ��, �� �Ǵ� ������ ���� ������ �Ⱓ�� ������ �������� �ƴ��Ѵ�. ���� �� �Ⱓ�� ���� ���÷κ��� �����ϴ� ������ ������ �ƴ��ϴ�.
��158��(������ �����)
���ɰ�꿡�� ������� �����Ѵ�.
��159��(�Ⱓ�� ������)
�Ⱓ�� ��, ��, �� �Ǵ� ������ ���� ������ �Ⱓ������ ����� �Ⱓ�� �����Ѵ�.
��160��(���� ���� ���)
��Ⱓ�� ��, �� �Ǵ� ������ ���� ������ ���� ���Ͽ� ����Ѵ�.
����, �� �Ǵ� ���� ó�����κ��� �Ⱓ�� ������� �ƴ��ϴ� ������ ������ ��, �� �Ǵ� ������ �� ����Ͽ� �ش��� ���� ���Ϸ� �Ⱓ�� �����Ѵ�.