Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

기록 저장소

[Java #22] util package 본문

kitri 노트/java

[Java #22] util package

resault 2019. 3. 27. 12:24

[ Calendar Class ]

1. 인스턴스 생성

1) 하위 클래스인 GregorianCalendar Class 이용
import java.util.Calendar;
public class CalendarTest {
    public static void main(String[] args) {
        Calendar today = new GregorianCalendar();
     }
}

 2) 자기 자신을 static으로 리턴하는 method 이용    (static은 객체 생성 없이 클래스 이름으로 바로 호출 가능)
import java.util.Calendar;
public class CalendarTest {
      public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
    }
}


2. method

1) get(int field)
- 해당 필드의 값을 int로 반환
int y = cal.get(cal.YEAR);
int m = cal.get(cal.MONTH)+1; //MONTH는 january가 0이므로 +1해야함

▷ 예제
//2019년 03월 26일 오후 01시 07분 35초
import java.util.Calendar;
public class CalendarTest {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        int y = cal.get(cal.YEAR);
        int m = cal.get(cal.MONTH)+1;
        int d = cal.get(cal.DAY_OF_MONTH);
        int apm = cal.get(cal.AM_PM);
        int h = cal.get(cal.HOUR);
        int mi = cal.get(cal.MINUTE);
        int s = cal.get(cal.SECOND);
                  
    System.out.println(y + "년 " + zero(m) + "월 " + zero(d) + "일 " +
                  (apm == 0 ? "오전" : "오후") + " "+ zero(h) + "시 "+  zero(mi) + "분 " + zero(s) + "초");
    }
private static String zero(int num) {
    return num < 10 ? "0" + num : "" + num;
}


[ Date Class ]

1) toString()
2) format()

▷ 예제
// 2019.03.26 14:25:30
Date date = new Date();
System.out.println(date); // toString() 생략

Format f = new SimpleDateFormat("yyyy.MM.dd HH:m:ss");
//Format f = new SimpleDateFormat("yyyy/MM/dd HH:m:s");
//Format f = new SimpleDateFormat("yyyy-MM-dd HH:m:s");
//Format f = new SimpleDateFormat("yyyy MM dd HH:m:s");
//Format f = new SimpleDateFormat("yyyy년 MM월 dd일 HH:m:s");  //but 웹상에서는 한글로 잘 안함

String today = f.format(date);
System.out.println(today);


[ Format Class ]

1)  number format()
package com.kitri.util;
import java.text.DecimalFormat;
public class NumberFormatTest {
      public static void main(String[] args) {
            
            double number = 563242365453.126;
            
            //double number = 34,563,242,365,453.13
            
            DecimalFormat num = new  DecimalFormat("###,###,###,###,###.##");
            String str = num.format(number);
            System.out.println(str);
            
      }
}


[ Random Class ]


1. method

1) nextBoolean() : 무작위로 참/거짓
boolean b = random.nextBoolean();
System.out.println(b);

2) nextDouble()
- double 범위 내에서 무작위로 double 값 생성
double d = random.nextDouble();
System.out.println("d == " + d);

3) nextInt()
- int 범위 내에서 무작위로 int 값 생성
int i = random.nextInt();
System.out.println("i == " + i);

4) nextInt(int bound)
- 0부터 인자값 -1 까지 사이의 범위 내에서 무작위로 int 값 생성 (가장 많이 사용)
int r = random.nextInt(256);
System.out.println("r == " + r);


[ StringTokenizer Class ]

* token : 의미를 가진 최소의 단위

1. 생성자

1) StringTokenizer(String str) 생성자    (delim의 default는 공백기준임)
String str = "hello java !!!";
StringTokenizer st = new StringTokenizer(str);

2) countTokens()
String str = "hello java !!!";
int cnt = st.countTokens();
System.out.println("토큰수 : " + cnt);

3) hasMoreElements() | hasMoreTokens()
4) nextElement() | nextToken()
String str = "hello java !!!";
while(st.hasMoreTokens()) {
      System.out.println(st.nextToken());
}
// 결과
// hello
// java
// !!!


▷ 예제
str = "TO|안효인|안녕하세요";
StringTokenizer st2 = new StringTokenizer(str, "|");
String protocol = st2.nextToken();
String to = st2.nextToken();
String msg = st2.nextToken();
System.out.println("기능 : "+ protocol);
System.out.println("누구에게 : "+ to);
System.out.println("보내는 메세지 : " + msg);



[ UUID Class ]

1)  randomUUID()
System.out.println(UUID.randomUUID());




'kitri 노트 > java' 카테고리의 다른 글

JDBC  (0) 2019.04.09
JTable - DefaultTableModel 사용  (0) 2019.04.08
[Java #21] lang package_StringBuffer Class  (0) 2019.03.27
[Java #20] lang package_String Class  (0) 2019.03.25
[Java #19] lang package_Wrapper Class-수정  (0) 2019.03.25