JAVA chapter11. 기본 API 클래스. 11.15 Format 클래스
11.15 Format 클래스
- 숫자와 날짜를 원하는 형식의 문자열로 변환
// 형식 클래스는 java.text 패키지에 포함되어 있다.
- 종류
// 숫자 형식 : DecimalFormat
// 날짜 형식 : SimpleDateFormat
// 매개변수화된 문자열 형식 : MessageFormat
11.15.1 숫자 형식 클래스(DecimalFormat)
기호 | 의미 | 패턴 예 | 1234567.89 -> 변환 결과 |
0 | 10진수(빈자리는 0으로 채움) | 0 0.0 0000000000.00000 | 1234568 1234567.9 0001234567.89000 |
# | 10진수(빈자리는 채우지 않음) | # #.# ##########.##### | 1234568 1234567.9 1234567.89 |
. | 소수점 | #.0 | 1234567.9 |
- | 음수 기호 | +#.0 -#.0 | +1234567.9 -1234567.9 |
, | 단위 구분 | #,###.0 | 1,234,567,9 |
E | 지수문자 | 0.0E0 | 1.2E6 |
; | 양수와 음수의 패턴을 모두 기술할 경우, 패턴 구분자 | +#,###;-#,### | +1,234,568(양수일 때) -1,234,568(음수일 때) |
% | 100을 곱한 후에 % 문자 붙임 | #.#% | 123456789 % |
\u00A4 | 통화 기호 | \n00A4. #,### | \ 1,234,568 |
- format() 메소드를 호출해서 패턴이 적용된 문자열얻기
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 43 44 45 46 47 48 49 50 51 | package sec15.exam01_format; import java.text.DecimalFormat; public class DecimalFormatExample { public static void main(String[] args) { double num = 1234567.89; DecimalFormat df = new DecimalFormat("0"); System.out.println( df.format(num) ); // 1234568 df = new DecimalFormat("0.0"); System.out.println( df.format(num) ); // 1234567.9 df = new DecimalFormat("0000000000.00000"); System.out.println( df.format(num) ); // 0001234567.89000 df = new DecimalFormat("#"); System.out.println( df.format(num) ); // 1234568 df = new DecimalFormat("#.#"); System.out.println( df.format(num) ); // 1234567.9 df = new DecimalFormat("##########.#####"); System.out.println( df.format(num) ); // 1234567.89 df = new DecimalFormat("#.0"); System.out.println( df.format(num) ); // 1234567.9 df = new DecimalFormat("+#.0"); System.out.println( df.format(num) ); // +1234567.9 df = new DecimalFormat("-#.0"); System.out.println( df.format(num) ); // -1234567.9 df = new DecimalFormat("#,###.0"); System.out.println( df.format(num) ); // 1,234,567,9 df = new DecimalFormat("0.0E0"); System.out.println( df.format(num) ); // 1.2E6 df = new DecimalFormat("+#,### ; -#,###"); System.out.println( df.format(num) ); // +1,234,568 df = new DecimalFormat("#.# %"); System.out.println( df.format(num) ); // 123456789 % df = new DecimalFormat("\u00A4 #,###"); System.out.println( df.format(num) ); // \ 1,234,568 } } | cs |
11.15.2 날짜 형식 클래스(SimpleDateFormat)
// Date 클래스의 toString() 메소드는 영문으로된 날짜를 리턴.
// 특정 문자열 포맷으로 얻고 싶다면 java.text.SimpleDateFormat 클래스를 이용한다
1 2 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일"); String strDate = sdf.format(new Date()); // format() 메소드를 호출해서 패턴이 적용된 문자열을 얻으면 된다. | 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 | package sec15.exam01_format; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample { public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println( sdf.format(now) ); sdf = new SimpleDateFormat("yyyy년 MM월 dd일"); System.out.println( sdf.format(now) ); sdf = new SimpleDateFormat("yyyy.MM.dd a HH:mm:ss"); System.out.println( sdf.format(now) ); sdf = new SimpleDateFormat("오늘은 E요일"); System.out.println( sdf.format(now) ); sdf = new SimpleDateFormat("올해의 D번째 날"); System.out.println( sdf.format(now) ); sdf = new SimpleDateFormat("이달의 d번째 날"); System.out.println( sdf.format(now) ); } } | cs |
11.15.3 문자열 형식 클래스(MessageFormat)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package sec15.exam01_format; import java.text.MessageFormat; public class MessageFormatExample { public static void main(String[] args) { String id = "java"; String name = "신용권"; String tel = "010-123-5678"; String text = "회원 ID: {0} \n회원 이름: {1} \n회원 전화: {2}"; String result1 = MessageFormat.format(text, id, name, tel); System.out.println(result1); System.out.println(); String sql = "insert into member values( {0}, {1}, {2} )"; Object[] arguments = { "'java'", "'신용권'", "'010-123-5678'" }; String result2 = MessageFormat.format(sql, arguments); System.out.println(result2); } } | cs |