JAVA Basic 강의자료] Calendar 클래스

 

JAVA Basic 강의자료] Calendar 클래스

 

 

 

실무개발자를위한 실무교육 전문교육센터학원
www.oraclejava.co.kr에 오시면 보다 다양한 강좌를 보실 수 있습니다.

 

 

 

Calendar 클래스

 

 

*java.util.Calendar는 날짜와 관련된 기능을 제공한다.


*시간과 날짜 정보 관리

-년, 월, 일, 요일, 시간, 분, 초, 밀리초, 오전 오후 등
-Calendar 클래스의 각 요소들을 설정하기나 알아내기 위한 필드들

 

 

 

 

 

*Calendar 객체 생성
-Calendar는 추상 클래스이므로 new Calendar() 하지 않음
-Calendar now = Calendar.getInstance(); 이용
-now객체는 현재 날짜와 시간 정보를 가지고 생성됨


*현재 날짜와 시간
-int year = now.get(Calendar.YEAR);  // 현재 년도
-int month = now.get(Calendar.MONTH) + 1; // 현재 달

 

*날짜와 시간 설정하기
-내가 관리할 날짜와 시간을 Calendar객체를 이용하여 저장
-Calendar 객체에 날짜와 시간을 설정한다고 해서 컴퓨터의 날짜와 시간을 바꾸지는 못함
-컴퓨터의 시간과 날짜를 바꾸는 다른 방법 이용 

 

 

 

 

 

 

 

<소스코드>

 

import java.util.Calendar;
public class CalendarEx {
 public static void printCalendar(String msg, Calendar cal) {
     int year = cal.get(Calendar.YEAR);

      // get()은 0~30까지의 정수 리턴.
     int month = cal.get(Calendar.MONTH) + 1;
      int day = cal.get(Calendar.DAY_OF_MONTH);
     int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
     int hour = cal.get(Calendar.HOUR);    
     int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
     int ampm = cal.get(Calendar.AM_PM);
     int minute = cal.get(Calendar.MINUTE);    
     int second = cal.get(Calendar.SECOND);
     int millisecond = cal.get(Calendar.MILLISECOND);
    
     System.out.print(msg + year + "/" + month + "/" + day + "/");
     switch(dayOfWeek) {
      case Calendar.SUNDAY : System.out.print("일요일"); break;    
      case Calendar.MONDAY : System.out.print("월요일"); break;
      case Calendar.TUESDAY : System.out.print("화요일"); break;
      case Calendar.WEDNESDAY : System.out.print("수요일"); break;
      case Calendar.THURSDAY : System.out.print("목요일"); break;
      case Calendar.FRIDAY: System.out.print("금요일"); break;
      case Calendar.SATURDAY : System.out.print("토요일"); break;    
     } 

 

=====================================================

 

 

      System.out.print("(" + hourOfDay + "시)");
     if(ampm == Calendar.AM) System.out.print("오전");
    
    else System.out.print("오후");      
     System.out.println(hour + "시 " + minute + "분 " +
      second + " 초 "   + millisecond + " 밀리초 ");
 }

  public static void main(String[] args) {

     Calendar now = Calendar.getInstance();
     printCalendar("현재 ", now);
 
     Calendar firstDate = Calendar.getInstance();
     firstDate.clear();
 
      // 2012년 12월 25일. 12월을 표현하기 위해 month에 11로 설정
     firstDate.set(2012, 11, 25);
     firstDate.set(Calendar.HOUR_OF_DAY, 20); // 저녁 8시
     firstDate.set(Calendar.MINUTE, 30); // 30분
     printCalendar("처음 만난 날은 ", firstDate);    
  }
} 

+ Recent posts