본문 바로가기

java 프로그래밍

제24장 Date 클래스 / Calendar 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Date 클래스에는 여러개의 생성자가 선언되어 있지만 대부분 Deprecated (비권장) 되어 현재는 Date()생성자만 쓴다.
Date()생성자는 컴퓨터의 현재 날씨를 읽어 Date객체로 만든다.
 
Date now = new Date();
 
package son;
 
import java.util.*;
 
public class DateE {
 
    public static void main(String[] args) {
    Date now = new Date();
    System.out.println(now);
    }
 
}
 
출력값
Fri Apr 12 23:29:58 KST 2019
 
 
Calendar 클래스는 달력을 표현한 클래스이다. Calendar 클래스는 추상(abstract)클래스 이므로 new 연산자를 
사용해서 인스턴스를 생성할수 없다. 그 이유는 날짜와 시간을 계산하는 방법이 지역과 문화, 나라에 따라 다르기 때문이다.
그래서 특별한 역법을 사용하는 경우가 아니라면 직접하위 클래스를 만들 필요 없이 Calendar 클래스의
정적 메소드인 getInstance()를 이용하여 현재 운영체제에 설정되어 있는 시간대 (Time Zone)를 기준으로한 Calendar 하위 객체를 얻을수 있다.
 
Calendar now = Calendar.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);//초를 리턴
 
package son;
public class CalendarE {
    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        
        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);
        String strWeek = null;
        
        switch(week) {
        case Calendar.MONDAY:
        strWeek = "월";
        break;
        
        case Calendar.TUESDAY:
        strWeek = "화";
        break;
        
        case Calendar.WEDNESDAY:
        strWeek = "수";
        break;
        
        case Calendar.THURSDAY:
        strWeek = "목";
        break;
        
        case Calendar.FRIDAY:
        strWeek = "금";
        break;
        
        case Calendar.SATURDAY:
        strWeek = "토";
        break;
        
        default:
        strWeek = "일";
        }
        
        
        
        
        int amPm = now.get(Calendar.AM_PM);
        String strAmPm = null;
        if(amPm == Calendar.AM) {
            strAmPm = "오전";
        }else {
            strAmPm = "오후";
        }
        
        int hour = now.get(Calendar.HOUR);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        
        
        System.out.print(year+"년");
        System.out.print(month+"월");
        System.out.println(day+"일");
        System.out.print(strWeek+"요일");
        System.out.println(strAmPm+"");
        System.out.print(hour+"시");
        System.out.print(minute+"분");
        System.out.println(second+"초");
        
    }
}
 
출력값
2019년4월13일
토요일오전
1시2분19초
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

'java 프로그래밍' 카테고리의 다른 글

제 26장 제네릭 타입  (0) 2019.04.15
제 25장 제네릭 기본 개념  (0) 2019.04.14
제 23 장 Wrapper클래스  (0) 2019.04.11
제 22장 인터페이스 구현  (0) 2019.04.08
제 21 장 인터페이스  (0) 2019.04.08