Date 클래스
- 특정 시점의 날짜를 표현하는 클래스
- Date 객체 안에는 특정 시점의 연도, 월, 일, 시간 정보가 저장된다.
Date now = new Date();
Calendar 클래스
- 달력을 표현한 클래스
- 추상 클래스이므로 new 연산자를 사용해서 인스턴스를 생성할 수 없다.
- Calendar 클래스의 정적 메소드인 getInstance() 메소드를 이용하면 현재 운영체제에 설정되어 있는 시간대를 기준으로한 Calendar 하위 객체를 얻을 수 있음
Calendar now = Calendar.getlnstance();
- Calendar 객체를 얻으면, 연도, 월, 일, 요일, 오전/오후, 시간 등의 정보를 얻을 수 있음
[확인 문제]
package yeong.verify.exam01;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DatePrintExample {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 E요일 HH시 mm분");
System.out.println( sdf.format(now) );
}
}
package sec02.verify.exam02;
import java.util.Calendar;
public class DatePrintExample {
public static void main(String[] args) {
Calendar now = Calendar.getlnstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
String strMonth = (month<10)? ("0"+month) : (""+month);
int dayOfMonth = now.get(Calendar.DAY_OF_MONTH);
String strDayOfMonth = (dayOfMonth<10)? ("0"+dayOfMonth) : (""+dayOfMonth);
String[] dayArray = {"일", "월’,, "화", "수,,, "목", "금", "토"};
int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
String strDayOfWeek = dayArray[dayOfWeek-1] + "요일";
int hour = now.get(Calendar.HOUR_OF_DAY);
String strHour = (hour<10)? ("0”+hour) : (",,+hour);
int second = now.get(Calendar.SECOND);
String strSecond = (second<10)? ("0"+second) : (""+second);
System.out.print(year + "년 ");
System.out.print(strMonth + "월 ");
System.out.print(strDayOfMonth + "일 ");
System.out.print(strDayOfWeek + " ");
System.out.print(strHour + "시 ");
System.out.print(strSecond + "분 ");
}
}
'JAVA' 카테고리의 다른 글
스레드 제어 (0) | 2022.12.05 |
---|---|
멀티 스레드 (0) | 2022.12.05 |
java.lang 패키지 (2) | 2022.11.30 |
익명 객체 (0) | 2022.11.15 |
중첩 클래스와 중첩 인터페이스 (0) | 2022.11.15 |