반응형 자바 날짜계산2 자바(JAVA) SimpleDateFormat을 이용한 날짜 계산 //날짜 클래스 생성 Date curDate = new Date(); //날짜형식 변환 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); sdf.format(curDate); //현재날짜 출력 System.out.println("today : " + sdf.format(curDate)); //어제날짜 계산 Date yesterDay = new Date(); yesterDay.setTime(curDate.getTime() - ((long)(1000 * 60 * 60 * 24) * 2)); //어제날짜 출력 System.out.println("yesterDay : " + sdf.format(yesterDay)); 2013. 11. 12. 자바(JAVA) 캘린더 클래스를 이용한 날짜 계산 //날짜 클래스 생성 Date curDate = new Date(); Calendar cal = Calendar.getInstance(); //현재날짜 셋팅 cal.setTime(curDate); //현재년도 출력 System.out.println(cal.get(Calendar.YEAR)); //월 계산 cal.add(Calendar.MONTH, 2); //계산된 월 출력 System.out.println(cal.get(Calendar.MONTH)); //일자 계산 cal.add(Calendar.DAY_OF_MONTH, 5); //일자 출력 System.out.println(cal.get(Calendar.DAY_OF_MONTH)); 2013. 11. 12. 이전 1 다음 반응형