● Calendar 클래스 : 추상클래스 => 직접 객체생성 X, 메서드를 통해 인스턴스를 얻음.

Calendar cal = new Calendar(); 			// X
Calendar today = Calendar.getInstance();	// O
package ch10;

import java.util.Calendar;

public class CalendarEx1 {

	public static void main(String[] args) {
		Calendar today = Calendar.getInstance();		// 인스턴스 반환.
		System.out.println("올해 년도 : " + today.get(Calendar.YEAR));		// 2020년
		System.out.println("달 : " + (today.get(Calendar.MONTH)+1));			// 5월
		System.out.println("년도기준 몇째주 : " + today.get(Calendar.WEEK_OF_YEAR));	//22째주
		System.out.println("달기준 몇째주 : " + today.get(Calendar.WEEK_OF_MONTH));	// 5째주
		System.out.println("일자 : " + today.get(Calendar.DATE));					// 27일
		System.out.println("일자 : " + today.get(Calendar.DAY_OF_MONTH));			// 27일
		System.out.println("년도기준날짜 : " + today.get(Calendar.DAY_OF_YEAR));	//148일째
		System.out.println("요일(1:일~7:토) : " + today.get(Calendar.DAY_OF_WEEK));		//4(수)
		System.out.println("월기준 몇째요일 : " + today.get(Calendar.DAY_OF_WEEK_IN_MONTH));	//4(수)
		System.out.println("오전(0)_오후(1) : " + today.get(Calendar.AM_PM));			// 0(오전)
		System.out.println("시간(0~11) : " + today.get(Calendar.HOUR));			// 1시
		System.out.println("시간(0~23) : " + today.get(Calendar.HOUR_OF_DAY));	// 13시
		System.out.println("분(0~59) : " + today.get(Calendar.MINUTE));			// 24분
		System.out.println("초(0~59) : " + today.get(Calendar.SECOND));			// 31초
		System.out.println("밀리초(0~999) : " + today.get(Calendar.MILLISECOND));	//542밀리초
		System.out.println("TimeZone(-12~12) : " + today.get(Calendar.ZONE_OFFSET/60*60*1000));		// 1
		System.out.println("이달의 마지막 날 : " + today.getActualMaximum(Calendar.DATE));		// 31일
		
		Calendar cal = Calendar.getInstance();
		cal.set(2019, (12-1), 30);
		System.out.println("날짜 : " + cal.get(Calendar.YEAR) + "년 " + (cal.get(Calendar.MONTH)+1)+ "월 " 
						+ cal.get(Calendar.DATE) + "일");
		cal.set(2019, (12-1), 31);
		
		String week = null;
		switch (cal.get(Calendar.DAY_OF_WEEK)) {
		case 1: week = "일"; break;
		case 2: week = "월"; break;
		case 3: week = "화"; break;
		case 4: week = "수"; break;
		case 5: week = "목"; break;
		case 6: week = "금"; break;
		case 7: week = "토"; break;
		}		
		System.out.println("마지막날짜 : " + cal.get(Calendar.YEAR) + "년 " + (cal.get(Calendar.MONTH)+1)+ "월 " 
						+ cal.get(Calendar.DATE) + "일 " + week + "요일");
	}

}

 

● SimpleDateFormat 클래스 : 날짜 데이터를 원하는 형태로 다양하게 출력

public static void main(String[] args) {
    Date today = new Date();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS a");

    System.out.println(df.format(today));
}

'자료 > 자바의 정석' 카테고리의 다른 글

CH13 쓰레드  (0) 2020.06.05
CH11 컬렉션  (0) 2020.05.28
CH9 기본API(1)  (0) 2020.05.25
CH8 예외처리  (0) 2020.05.22
CH7 객체지향 프로그래밍(2)  (0) 2020.05.18

+ Recent posts