20일차 강의 정리


1. Calendar Class

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
import java.util.Calendar;
 
public class Ex02 {
    public static void main(String[] args) {
        //캘린더
        Calendar now;
        now = Calendar.getInstance();
        
        int year = now.get(Calendar.YEAR);
        System.out.print(year+"년 ");
        int month = now.get(Calendar.MONTH);
        System.out.print(month+1+"월 ");
        int day = now.get(Calendar.DATE);
        System.out.print(day+"일 ");
 
        int week = now.get(Calendar.DAY_OF_WEEK);
        char[] weeks={'일','월','화','수','목','금','토'};
        System.out.println(weeks[week-1]+"요일");
        
        int ampm = now.get(Calendar.AM_PM);
        if(ampm==0){
            System.out.print("AM ");
        }else{
            System.out.print("PM ");
        }
        int hour = now.get(Calendar.HOUR);
        System.out.print(hour+":");
        int min = now.get(Calendar.MINUTE);
        System.out.print(min+":");
        int sec = now.get(Calendar.SECOND);
        System.out.println(sec);
        
        int hhour = now.get(Calendar.HOUR_OF_DAY);
        System.out.println(hhour+":"+min+":"+sec);
        
        int cntDay = now.get(Calendar.DAY_OF_YEAR);
        System.out.println(cntDay+"일째 입니다.");
        
        now.set(year, month, 4);
 
    }
}
cs

결과


2. Date Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Ex03 {
    public static void main(String[] args) {
        //Date
        Date now = new Date();
        System.out.println(now);
        System.out.println(now.toString());
        System.out.println("-------------------------------");
        
        SimpleDateFormat sdf;
        sdf = new SimpleDateFormat("yyyy년 mm월 dd일 hh시 mm분 ss초");
        String kor = sdf.format(now);
        System.out.println(kor);
        System.out.println(now.getMonth());    //월 은 0부터시작해서 이번달이 7월이면 6이 출력
        System.out.println(now.getTime());
 
    }
 
}
cs

결과


3. Random Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Random;
 
public class Ex04 {
 
    public static void main(String[] args) {
        //Random
        double ran = Math.random();    
        System.out.println(ran);
        
        Random ran2 = new Random();
        System.out.println(ran2.nextInt());        //int자료형의 표현 범위 내에서 난수발생
        System.out.println(ran2.nextLong());    //long자료형의 표현 범위 내에서 난수발생
        System.out.println(ran2.nextDouble());    //= Math.random()
        //0~9난수
        System.out.println(ran2.nextInt(10));     //0<=ran2<인자
        //1~45
        System.out.println(ran2.nextInt(45)+1);
    }
 
}
cs

결과


4. Arrays Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Arrays;
 
public class Ex05 {
    public static void main(String[] args) {
        //Arrays
        int[] arr1 = {30,45,20,3,1,35};
        Arrays.sort(arr1);        //정렬
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]+" ");
        }
        System.out.println();
 
        int[] arr2 = new int[10];
        Arrays.fill(arr2, 11);
        Arrays.fill(arr2, 3,5,3);         //arr2에서 자리번호 3~5까지 2로 채워넣는다
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i]+" ");
        }
    }
}
cs

결과


응용 소스

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
import java.util.Arrays;
 
public class Ex06 {
    public static void main(String[] args) {
        
        int[] lotto = {30,45,20,3,1,35};
        Arrays.sort(lotto);        //정렬
        int idx = Arrays.binarySearch(lotto, 30);
        
        System.out.println("30의 자리위치: "+idx);
        int[] arr2 = Arrays.copyOfRange(lotto, 1,3);    //복사-> 자리번호1에서 3이전까지
        System.out.println(Arrays.toString(arr2));        //복사한것 출력
        
        System.out.println("====================================");
        String[] names={"홍길동","김만수","김철수","고경아"};
        System.out.println(Arrays.toString(names));    //정렬 전
        Arrays.sort(names);        //binarySearch하기위해 필요(정렬)
        int idx2 = Arrays.binarySearch(names, "김만수");
        System.out.println("\'김만수\'의 자리 위치: "+idx2);
        System.out.println(Arrays.toString(names));    //정렬 후
        
        System.out.println("====================================");
        System.out.println("====================================");
//        String[] arr = Arrays.copyOf(names, names.length);    //복사, 몇번까지
        String[] arr = Arrays.copyOfRange(names, 1,3);        //복사 자리번호1에서 3전까지
        names[1]="바꿈";
        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(names));
 
    }
}
cs

결과


'* Programming > JAVA' 카테고리의 다른 글

DAY21 컬렉션 프레임워크  (0) 2016.08.03
DAY21 싱글톤 패턴  (0) 2016.08.02
DAY20 Object클래스,리플렉션  (0) 2016.08.02
DAY20 this/super  (0) 2016.08.02
DAY19 익명클래스  (0) 2016.08.02

+ Recent posts