[Java][time] 형식화 클래스 : DecimalFormat, SimpleDateFormat, ChoiceFormat, MessageFormat

2022. 6. 16. 15:07JAVA/Language

1. DecimalFormat

DecimalFormat 클래스는 형식화 클래스 중에서 숫자를 형식화하는데 사용되는 클래스입니다.

import java.text.DecimalFormat;

public class DecimalFormatEx1 {
	public static void main(String[] args) {
		double number = 1234567.89;
		String[] pattern = {
				"0",
				"#",
				"0.0",
				"#.#",
				"0000000000.0000",
				"##########.####",
				"#.#-",
				"-#.#",
				"#,###.##",
				"#,####.##",
				"#E0",
				"0E0",
				"##E0",
				"00E0",
				"####E0",
				"0000E0",
				"#.#E0",
				"0.0E0",
				"0.000000000E0",
				"00.00000000E0",
				"000.0000000E0",
				"#.#########E0",
				"##.########E0",
				"###.#######E0",
				"#,###.##+;#,###.##-",
				"#.#%",
				"#.#\u2030",
				"\u00A4 #,###",
				"`#`#,###",
				"``#,###",
		};
		
		for(int i = 0; i < pattern.length; i++) {
			DecimalFormat df = new DecimalFormat(pattern[i]);
			System.out.printf("%19s : %s\n", pattern[i], df.format(number));
		}
	}
}
                  0 : 1234568
                  # : 1234568
                0.0 : 1234567.9
                #.# : 1234567.9
    0000000000.0000 : 0001234567.8900
    ##########.#### : 1234567.89
               #.#- : 1234567.9-
               -#.# : -1234567.9
           #,###.## : 1,234,567.89
          #,####.## : 123,4567.89
                #E0 : .1E7
                0E0 : 1E6
               ##E0 : 1.2E6
               00E0 : 12E5
             ####E0 : 123.5E4
             0000E0 : 1235E3
              #.#E0 : 1.2E6
              0.0E0 : 1.2E6
      0.000000000E0 : 1.234567890E6
      00.00000000E0 : 12.34567890E5
      000.0000000E0 : 123.4567890E4
      #.#########E0 : 1.23456789E6
      ##.########E0 : 1.23456789E6
      ###.#######E0 : 1.23456789E6
#,###.##+;#,###.##- : 1,234,567.89+
               #.#% : 123456789%
               #.#‰ : 1234567890‰
            ¤ #,### : ? 1,234,568
           `#`#,### : `1,234,568`
            ``#,### : ``1,234,568

 

2. SimpleDateFormat

SimpleDateFormat 클래스는 Date클래스와 Calendar 클래스의 인스턴스를 형식화하여 날짜를 표현하는 클래스입니다.

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

public class DateFormatEx1 {

	public static void main(String[] args) {
		Date today = new Date();
		
		SimpleDateFormat[] sdfs = new SimpleDateFormat[9];
		
		sdfs[0] = new SimpleDateFormat("yyyy-MM-dd");
		sdfs[1] = new SimpleDateFormat("``yy년 MMM dd일 E요일");
		sdfs[2] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		sdfs[3] = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
		
		sdfs[4] = new SimpleDateFormat("오늘은 올 해의 D번째 날입니다.");
		sdfs[5] = new SimpleDateFormat("오늘은 이 달의 d번째 날입니다.");
		sdfs[6] = new SimpleDateFormat("오늘은 올 해의 w번째 주입니다.");
		sdfs[7] = new SimpleDateFormat("오늘은 이 달의 W번째 주입니다.");
		sdfs[8] = new SimpleDateFormat("오늘은 이 달의 F번째 E요일입니다.");
		
		for(SimpleDateFormat sdf : sdfs) {
			System.out.println(sdf.format(today));
		}
		
	}

}
2022-06-16
``22년 6월 16일 목요일
2022-06-16 15:01:10.044
2022-06-16 03:01:10 오후
오늘은 올 해의 167번째 날입니다.
오늘은 이 달의 16번째 날입니다.
오늘은 올 해의 25번째 주입니다.
오늘은 이 달의 3번째 주입니다.
오늘은 이 달의 3번째 목요일입니다.

 

3. ChoiceFormat

ChoiceFormat 클래스는 특정 범위에 속하는 값을 문자열로 변환해줍니다.

public class ChoiceFormatEx1 {

	public static void main(String[] args) {
		double[] limits = {60, 70, 80, 90};	// 오름차순이어야 함
		// limits 배열과 grades간의 순서와 개수가 일치해야함
		String[] grades = {"D", "C", "B", "A"};
		
		int[] scores = {100, 95, 88, 70, 52, 60, 70};
		
		ChoiceFormat cf = new ChoiceFormat(limits, grades);
		
		for(int score : scores) {
			System.out.println(score + ":" + cf.format(score));
		}
	}

}
100:A
95:A
88:B
70:C
52:D
60:D
70:C
  • 60 : 60~69에 해당하는 값은 D
  • 70 : 70~79에 해당하는 값은 C
  • 80 : 80~89에 해당하는 값은 B
  • 90 : 90~ 에 해당하는 값은 A

 

4. MessageFormat

MessageFormat 클래스는 데이터를 정해진 양식에 맞게 출력할 수 있도록 도와주는 클래스입니다.

public class MessageFormatEx1 {

	public static void main(String[] args) {
		String msg = "Name: {0} \nTel: {1} \nAge: {2} \nBirthday: {3}";
		
		Object[] arguments = {"이자비", "02-123-1234", "27", "07-09"};
		
		String result = MessageFormat.format(msg, arguments);
		System.out.println(result);
	}
}
Name: 이자비 
Tel: 02-123-1234 
Age: 27 
Birthday: 07-09

 

References

source code : https://github.com/yonghwankim-dev/java_study/tree/main/ch10
[도서] Java의 정석, 남궁 성 지음