컴퓨터 관련/JAVA 강의

java #21 꼭 알아야 할 클래스와 메소드-2- (날짜, 포메터 관련)

승명 2016. 12. 18. 19:53

java #21 꼭 알아야 할 클래스와 메소드-2- (날짜, 포메터 관련)

 

- 관련 Post

2015/01/17 - [프로그래밍/Java] - java #20 꼭 알아야 할 클래스와 메소드-1- (문자열 관련)

2015/01/17 - [프로그래밍/Java] - java #22 꼭 알아야 할 클래스와 메소드-3- (수학, 난수 관련)



* 날짜, 포메터 관련 클래스와 메소드

날짜 : Date, Calendar
포메터 : SimpleDateFormat, DecimalFormat

용도클래스특성예제

날짜

Date

- 형식이 있는 날짜정보 얻기
- 비추천 메소드지만, 원하는 날짜 형식 만들때 씀
- Calendar 클래스 사용을 추천함.

1
2
3
4
5
Date d = new Date();  //객체화
 
//원하는 날짜 형식 만드는 클래스 :
java.text.SimpleDateFormat
-> 단, 날짜 정보를 가지고 있지 않음.
cs

    
Calendar- 단편적인 날짜 정보
- 추상클래스, 객체화 안됨.
- 생성
1
2
3
4
5
6
7
8
- is a 관계 객체화
Calendar cal = new GregorianCalendar();
 
- 객체 얻는 메서드를 이용한 객체화
Calendar cal = Calendar.getInstance();
 
- 그레고리안카랜다로 객체화
GregorianCalendar gc=new GregorianCalendar();
cs


- 사용
1
2
3
4
5
6
7
8
9
10
11
12
13
- 년도(int)
cal.get(Calendar.YEAR);
 
- 월(int)
cal.get(Calendar.MONTH)+1; (0월~11월이 나오므로 1 더해야함)
 
- 일(int)
cal.get(Calendar.DAY_OF_MONTH);
cal.get(Calendar.DAY_OF_YEAR);
 
- 요일(int)
cal.get(Calendar.DAY_OF_WEEK);
cs

포메터

SimpleDateFormat- 패턴
y
M
h,H,K
m
s
오전오후a
요일E
- 생성
1
2
3
SimpleDateFormat sdf=new SimpleDateFormat(mm-dd-yyyy);
SimpleDateFormat sdf=new SimpleDateFormat(hh:mm:ss);
cs


- 사용
1
2
3
4
SimpleDateFormat sdf= new SimpleDateFormat
("yyyy.MM.dd a hh(HH,kk):mm:ss EEEE", Locale.KOREA);
 
System.out.println(sdf.format(date));
cs
Decimalformat- 특정자리에 ,를 찍을때
- java.text
- 패턴
#해당 자리에 데이터가 없으면 출력안함
0해당 자리에 데이터가 없으면 0을 출력
- 생성 및 사용
1
2
3
DecimalFormat df = new DecimalFormat("#,###,###");
int i=2014;
s.o.p(df.format(i)); // 결과 : 2,014
cs

1
2
3
DecimalFormat df = new DecimalFormat("0,000,000");
int i=2014;
s.o.p(df.format(i)); // 결과 : 0,002,014
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
package day0228;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
/**
* 날짜정보를 얻을 수 있는 클래스
*/
public class UseDate {
     public UseDate(){
          Date date=new Date();
          System.out.println(date);
 
          //원하는 형식의 날짜정보를 만들기 위해 Simple Date format 클래스 사용         
          SimpleDateFormat sdf= new SimpleDateFormat
                       ("yyyy.MM.dd a hh(HH,kk):mm:ss EEEE", Locale.KOREA);
          System.out.println(sdf.format(date));
     }
public static void main(String[] args){
 
     //UseDate ud = new UseDate();
     //ud.mathod();
     new UseDate();
}
}
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
32
33
34
35
36
37
38
39
40
41
package day0228;
 
import java.util.Calendar;
import java.util.GregorianCalendar;
 
/**
* 날짜 정보 얻기(단편적인 날짜)
*/
public class UseCalendar {
     public UseCalendar() {
          Calendar cal = Calendar.getInstance();
          Calendar cal1 = new GregorianCalendar();
          GregorianCalendar gc = new GregorianCalendar();
          System.out.println(cal);
          System.out.println(cal1);
          System.out.println(gc);
          int year = cal.get(Calendar.YEAR);
          int month = cal.get(Calendar.MONTH) + 1;
          int day = cal.get(Calendar.DAY_OF_MONTH);
          int day_of_year = cal.get(Calendar.DAY_OF_YEAR);
          int ampm = cal.get(Calendar.AM_PM);
          int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
          String[] week_title = { "일""월""화""수""목""금""토" };
          // 시분초
          int hour = cal.get(Calendar.HOUR_OF_DAY);
          int minute = cal.get(Calendar.MINUTE);
          int second = cal.get(Calendar.SECOND);
 
          System.out.println(year + " / " + month + " / " + day + " / "
                    + day_of_year + " / " + ((ampm == 0) ? "오전" : "오후") + " / "
                    + week_title[day_of_week - 1] + " / " + hour + ":" + minute
                    + ":" + second);
         
          int temp_year=2014;
          System.out.println(temp_year+"년은 "+(gc.isLeapYear(temp_year)?"윤년":"평년"));
     }// UseCalendar
 
     public static void main(String[] args) {
          new UseCalendar();
     }// main
}// class
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package day0228;
 
import java.text.DecimalFormat;
 
/**
* 숫자 포멧 사용
*/
public class UseDecimalFormat {
 
     public UseDecimalFormat() {
          int i = 201402;
          DecimalFormat df = new DecimalFormat("#,###,###");
          DecimalFormat df1 = new DecimalFormat("0,000,000");
          System.out.println("# 사용 " + df.format(i));
          System.out.println("0 사용 " + df1.format(i));
     }// UseDecimalFormat
 
     public static void main(String[] args) {
          new UseDecimalFormat();
     }// main
 
}// class