세로형
Recent Posts
Recent Comments
Link
04-24 04:28
«   2024/04   »
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
Archives
Today
Total
관리 메뉴

꿈 많은 사람의 이야기

자바 날짜(Date, Calendar) 클래스 본문

java

자바 날짜(Date, Calendar) 클래스

이수진의 블로그 2017. 8. 18. 08:40

자바는 시스템의 날짜 및 시각을 읽을 수 있도록 Date와 Calendar 클래스를 제공하고 있다. 이 두 클래스는 모두 java.util 패키지에 포함되어 있다.

Date 클래스


Date는 날짜를 표현하는 클래스이다.  Date 클래스의 Date() 생성자는 컴퓨터의 현재 날짜를 읽어 Date 객체로 만든다

Date now = new Date();
현재 날짜를 문자열로 얻고 싶다면 toString() 메소드를 사용하면 된다. toString() 메소드는 영문으로 된 날짜를 리턴한다.
만약 특정 형태로 출력하고 싶으면 SimpleDateFormat 클래스를 이용하면 된다.

1
2
3
4
5
6
7
Date now = new Date();
String n = now.toString();
System.out.println(n);
        
SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 hh시 mm분 ss초");
String n2 = sdf.format(now);
System.out.println(n2);
Colored by Color Scripter

 

 


Date에 대한 간단한 예제는 이와 같다.

Calendar 클래스


Calendar 클래스는 달력을 표현한 클래스이다. Calendar 클래스는 추상 클래스라서 new 연산자를 이용해서 객체를 생성할 수 없다. 그래서 클래스의 정적 메소드인 getInstance() 메소드를 이용하면 현재 OS에 설정되어 있는 시간대를 기준으로 한 Calendar 하위 객체를 얻을 수 있다.

Calendar now = Calendar.getInstance();

Calendar 객체를 얻었다면 get()메소드를 이용해서 날짜와 시간에 대한 정보를 읽을 수 있다.

int year = now.get(Calendar.YEAR); 
년도 리턴
int month = now.get(calendar.MONTH) + 1
월 리턴
int day = now.get(Calendar.DAY_OF_MONTH);
일 리턴(해당 월의 일)
int day = now.get(Calendar.DATE);
일 리턴
int week = now.get(Calendar.DAY_OF_WEEK);
요일 리턴(일요일이 1)
int amPm = now.get(Calendar.AM_PM);
오전, 오후 리턴(오전 0)
int hour = now.get(Calendar.HOUR);
시 리턴(14시면 2로 )
int hour = now.get(Calendar.HOUR_OF_DAY);
시 리턴(14시면 14시로)
int minute = now.get(Calendar.MINUTE);
분 리턴
int second = now.get(Calendar.SECOND);
초 리턴
int dayOfYear = now.get(Calendar.DAY_OF_YEAR);
올해 몇 번째 날인지 출력(4월24일은 114번째 날)
int weekOfYear = now.get(Calendar.WEEK_OF_YEAR);
올해 몇 번째 주
int maxDay = now.getActualMaximun(Calendar.DATE);
이번달의 마지막 일(17년 6월이면  -> 30일)
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
 
 
 
public class CalEx {
 
    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH)+1;  //월은 -1 이 되서 나와서 +1을 해야 정확하게 나온다
        int day = now.get(Calendar.DAY_OF_MONTH);
        
        int week = now.get(Calendar.DAY_OF_WEEK);
        String w = null;
        
        switch(week)
        {
        case 1 :
            w = "일요일";
            break;
        case 2 :
            w = "월요일";
            break;
        case 3 :
            w = "화요일";
            break;
        case 4 :
            w = "수요일";
            break;
        case 5 :
            w = "목요일";
            break;
        case 6 :
            w = "금요일";
            break;
        case 7 :
            w = "토요일";
            break;
        }
        
        int amPm = now.get(Calendar.AM_PM);
        String ap = null;
        if(amPm == Calendar.AM)
        {
            ap = "오전";
        }
        else
            ap = "오후";
        
        
        int hour = now.get(Calendar.HOUR);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        
        
        System.out.println(year+ " 년 ");
        System.out.println(month+ " 월 ");
        System.out.println(day+ " 일 ");
        System.out.println(w+ " 요일 ");
        System.out.println(ap+ " ");
        System.out.println(hour+ " 시 ");
        System.out.println(minute+ " 분 ");
        System.out.println(second+ " 초 ");
        
    }
 
}
Colored by Color Scripter

위 소스 처럼 Calendar를 사용할 수 있다. 출력 결과는 다음과 같다. 
현재 시간을 기준으로 나오는 것이다.

 


1
2
3
4
5
6
7
8
9
10
11
12
int y = 2016;
int m = 03//4월을 구하기 위해. 0부터 시작하기 때문에
int d = 15;
Calendar c5 = Calendar.getInstance();
c5.set(y, m, d);
int year5 = c5.get(Calendar.YEAR);
//해당 날짜의 년 
int month5 = c5.get(Calendar.MONTH);
//해당 날짜의 월 
        
System.out.println(year5+", "+month5);
 
Colored by Color Scripter

 

 

위 예제 처럼 set()메소드를 사용해서 해당 년, 월에 대한 날짜를 셋팅할 수도 있다. 저렇게 하면 해당 셋팅한 날짜를 기준으로 날짜를 구할 수 있다.


반응형
그리드형
Comments