[Java] Class SimpleDateFormat

(Modify : 2017-09-18)

형식화 클래스

원하는 형식으로 표현 또는 변환하기 위해서 패턴을 정의한다.

형식화된 데이터에서 원래의 데이터를 얻을 수 있다.

package java.text에 포함되어 있다.

숫자, 날짜, 텍스트 데이터를 일정한 형식에 맞게 표현할 수 있는 방법을 객체지향적으로 설계하여 표준화했다.

Class SimpleDateFormat

날짜 데이터를 원하는 형태로 다양하게 출력할 수 있다.

사용법

원하는 출력형식의 패턴을 작성하여 SimpleDateFormat 인스턴스를 생성한다.

출력하고자 하는 Date 인스턴스를 가지고 format(Date d)를 호출하면 지정한 출력 형식에 맞게 변환된 문자열을 얻을 수 있다.

Java

1

2

3

4

Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");               
System.out.println(df.format(today));    // 2017-08-24

기호

  • G 연대 (BC, AD)

  • y 년도

  • M 월 (1~12 또는 1월~12월)

  • w 년의 몇 번째 주 (1~53)

  • W 월의 몇 번째 주 (1~5)

  • D 년의 몇 번째 일 (1~366)

  • d 월의 몇 번째 일 (1~31)

  • F 월의 몇 번째 요일 (1~5)

  • E 요일

  • a 오전/오후 (AM/PM)

  • H 시간 (0~23)

  • h 시간 (1~12)

  • k 시간 (1~24)

  • K 시간 (0~11)

  • m 분 (0~59)

  • s 초 (0~59)

  • S 1/1000초 (0~999)

  • z Time zone (General time zone)

  • Z Time zone (RFC 822 time zone)

  • escape 문자 (특수문자를 표현하는 데 사용)

Java

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

import java.util.Date;
import java.text.SimpleDateFormat;

public class Test {
    public static void main(String[] args) {

        Date today = new Date();

        print(new SimpleDateFormat("연대 G").format(today));                  // 연대 서기
        print(new SimpleDateFormat("y년도").format(today));                   // 2017년도
        print(new SimpleDateFormat("M월").format(today));                     // 8월
        print(new SimpleDateFormat("올 해의 D번째 날").format(today));        // 올 해의 236번째 날
        print(new SimpleDateFormat("이 달의 d번째 날").format(today));        // 이 달의 24번째 날
        print(new SimpleDateFormat("올 해의 w번째 주").format(today));        // 올 해의 34번째 주
        print(new SimpleDateFormat("이 달의 W번째 주").format(today));        // 이 달의 4번째 주
        print(new SimpleDateFormat("이 달의 F번째 E요일").format(today));     // 이 달의 4번째 목요일
        print(new SimpleDateFormat("오전/오후 : a").format(today));           // 오전/오후 : 오후
        print(new SimpleDateFormat("H시 (0~23)").format(today));              // 23시 (0~23)
        print(new SimpleDateFormat("h시 (1~12)").format(today));              // 11시 (1~12)
        print(new SimpleDateFormat("k시 (1~24)").format(today));              // 23시 (1~24)
        print(new SimpleDateFormat("K시 (0~11)").format(today));              // 11시 (0~11)
        print(new SimpleDateFormat("m분 (0~59)").format(today));              // 18분 (0~59)
        print(new SimpleDateFormat("s초 (0~59)").format(today));              // 31초 (0~59)
        print(new SimpleDateFormat("1/1000초 (0~999) : S").format(today));    // 1/1000초 (0~999) : 7
        print(new SimpleDateFormat("z").format(today));                       // GMT+09:00. Time zone(General time zone)
        print(new SimpleDateFormat("Z").format(today));                       // +0900. Time zone(RFC 822 time zone)
    }

    public static void print(String s){
        System.out.println(s);
    }
}

Java

1

2

3

4

print(new SimpleDateFormat("yyyy-MM-dd").format(today));                      // 2017-08-24
print(new SimpleDateFormat("yy년 MMM dd일 E요일").format(today));             // 17년 8월 24일 목요일
print(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(today));         // 2017-08-24 23:18:31.721
print(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a").format(today));           // 2017-08-24 11:18:31 오후

패턴에 기호 외의 다른 영문자가 들어가면 문자열을 얻지 못한다.

Date parse(String source)

문자열을 날짜(Date 인스턴스)로 변환한다.

Java

1

public Date parse(String source) throws ParseException

지정된 패턴으로 입력되지 않은 경우, parse 메서드를 호출하는 부분에서 예외(ParseException)가 발생한다.

Java

1

2

3

4

5

6

7

DateFormat df1 = new SimpleDateFormat("yyyy년 MM월 dd일");
DateFormat df2 = new SimpleDateFormat("yyyy.MM.dd");

try {
    Date date = df1.parse("2017년 08월 01일");
    System.out.println(df2.format(date));                      // 2017.08.01
} catch (Exception e) { }

날짜 데이터를 문자열로 입력 받을 때, 문자열을 날짜로 인식하기 위해 substring 메서드를 이용해 년, 월, 일을 뽑아야 하는 수고를 덜어준다.

SimpleDateFormat API

참고 서적: 자바의 정석 3판 2

민갤

Back-End Developer

백엔드 개발자입니다.

AMP 사이트에서 Google AdSense 검수받기

AMP사이트에서 Google AdSense 검수를 해야하는 사람들을 위한 안내서

Technique

[입문] 웹 프론트 엔드 - 언어의 문법

웹 프론트 엔드에서 사용하는 언어의 문법에 대한 강의 입니다.

Technique

v5.0