Java언어 스터디 Chapter11-2(java.util)

최재하·2022년 12월 12일
0

Date 클래스

Date클래스는 날짜를 표현하는 클래스로, 객체 간에 날짜 정보를 주고 받을 때 주로 사용됩니다.

Date now = new Date();

toStirng()메소드를 사용하면 현재날짜를 문자열로 얻을 수 있습니다.
하지만 toString()메소드는 영문으로 된 날짜를 리턴하기 때문에 원하는 날짜형식으로 받고싶다면 java.text.SimpleDateFormat클래스로 다음과 같이 생성 가능합니다.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초");

Calendar 클래스

Calendar클래스는 달력을 표현한 클래스입니다.
Calendar클래스는 추상 클래스이므로 new연산자를 사용해서 인스턴스를 생성할 수 없습니다.
Calender클래스의 getInstance()메소드를 사용하면 현재 운영체제에 설정되어있는 시간을 기준으로 한 Calendar하위 겍체를 얻을 수 있다.

Calender now = Calender.getInstance();

객체를 얻었다면 get()을 사용해서 날짜와 시간에대한 정보를 얻을 수 있습니다.

int year 	= now.get(Calendar.YEAR);			//년도를 리턴
int month 	= now.get(Calendar.MONTH) + 1;		//월을 리턴
int day 	= now.get(Calendar.DAY_OF_MONTH);	//일을 리턴
int week 	= now.get(Calendar.DAY_OF_WEEK);	//요일을 리턴
int amPm 	= now.get(Calendar.AM_PM);			//오전/오후를 리턴
int hour 	= now.get(Calendar.HOUR);			//시를 리턴
int minute 	= now.get(Calendar.MINUTE);			//분을 리턴
int second 	= now.get(Calendar.SECOND);			//초를 리턴

get()메소드를 호출할 때 사용한 매개값은 모두 Calendar클래스에 선언되어 있는 상수들입니다.

0개의 댓글