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
117
118
119
120
121
122
123
124
125
126
127
 
import java.util.Calendar;
import java.util.Date;
 
class DateTime 
{
    public static void main(String[] args) 
    {
        
        //m1();
        m2();
 
    }
    
    
    public static void m2()
    {
        //날짜 시간 연산
 
 
        //현재 시간
        Calendar now = Calendar.getInstance(); //-> 시간얻어오기
 
 
    
        //ask] 오늘로부터 100일 뒤는? -> 200일 뒤는?
        // void add(int field, int value) //반환값 없고 이런 매개변수를 사용
        now.add(Calendar.DATE, 100); 
        System.out.printf("%tF\n", now); // 2015-03-27
 
        //now.add(Calendar.DATE, 200);
        //System.out.printf("%tF\n", now); //주의! 이전값 03-27에 더해져서 300일이 출력됨
        
        now.add(Calendar.DATE, 100);
        System.out.printf("%tF\n", now); //그래서 값을 100으로
 
        //int n = 10;
        //n = 20;
 
 
 
        //ask] 지금으로부터 5시간 뒤는?
        now = Calendar.getInstance();
        now.add(Calendar.HOUR, 5); //5시간뒤?
        System.out.printf("%tF %tT\n", now, now); //형식문자 2개이면, 뒤에 글자도 2개로
 
 
 
        //ask]지금으로부터 100일전은?
        now = Calendar.getInstance(); 
        now.add(Calendar.DATE, -100);
        System.out.printf("%tF\n", now);
    
 
 
        //ask] 지금으로부터 크리스마스가 몇시간 남았는지? 
        //  - 크리스마스 - 현재시간 = ? (빼기)
        now = Calendar.getInstance();                    //현재시간
        Calendar christmas = Calendar.getInstance();    //크리스마스 자정
        christmas.set(20141125000);            //크리스마스가 되는 시점
 
        //christmas - now;                                //크리스마스-현재시간 = ?  //minus연산은 숫자만 돼서 에러가 뜸 -> 단위를 맞춰줘야 한다->틱값 구하기
        
        //*tick값 구하기(1970.01.01 자정 기준 -> 1/1000초) 
        //Date temp = now.getTime(); 
        //long n = temp.getTime();
        //System.out.println(n); //
 
        //*메소드 체이닝
        long christmasTick = christmas.getTime().getTime(); //getTime() 2개는 서로 다른 객체이다.
                                //->이 위치에 해당객체(Date)가 남음
        long nowTick = now.getTime().getTime();
 
        long gap = christmasTick - nowTick;                //둘의 차이를 구하기
 
        System.out.printf("올해 크리스마스는 총 %,d시간 남았습니다.\n", gap / 1000 / 60/ 60); 
                                                                              //시   분  초로 나눠주기
    }
    
    
    
    
    public static void m1()
    {
        //Calendar 객체 생성
        //1. 현재 시간 얻어오기(지금)
        //2. 특정 시간 얻어오기(내생일)
        //  - 현재시간 얻어온 후에 특정 시간 수정
 
        //특정 항목 가져오기
        //  - int get(int field)
        //특정 항목 수정하기(넣기)
        //  1. void set(int field, int value)
        //  2. void set(int year, int month, int day, int hour, int minute, int second) // 하나하나 쓰기 힘드니까 이런st로 오버로딩하기
        //  3. void set(int year, int month, int day)
 
 
 
        //현재시간 ask] 메소드
        Calendar now = Calendar.getInstance();
        //System.out.println(now);
        System.out.println(now.get(Calendar.YEAR));                // -> 1.읽기
 
        //특정시간 ask] 1990년 5월 20일생 출력하기
        Calendar birthday = Calendar.getInstance();
        birthday.set(Calendar.YEAR, 1990);                        // -> 2.쓰기(넣기), 수정하기
        birthday.set(Calendar.MONTH, 4); //5월생일 경우 -1, 0부터 시작
        birthday.set(Calendar.DATE, 20);
        birthday.set(Calendar.HOUR_OF_DAY, 11);
        birthday.set(Calendar.MINUTE, 30);
        birthday.set(Calendar.SECOND, 0);
 
        birthday.set(199042011300);  // 간단st. 오버로딩
        //System.out.println(burth.get(Calendar.YEAR));
        System.out.println(birthday);
 
 
        //출력]
        //prindf() 메소드에 대응하는 형식문자 
        System.out.printf("%tF\n", birthday); //%tF //1990-02-20
        System.out.printf("%tT\n", birthday); //11:30:00
        System.out.printf("%tA\n", birthday); //일요일
        System.out.printf("%tr\n", birthday); //11:30:30 오전
        System.out.printf("%tR\n", birthday); //11:30
    }
}
 
cs


'WEB > JAVA' 카테고리의 다른 글

SWITCH문  (0) 2015.05.22
IF문  (0) 2015.05.22
날짜&시간1  (0) 2015.05.22
메소드6 (예제)  (0) 2015.05.22
메소드5 (재귀 메소드)  (0) 2015.05.22

+ Recent posts