Java 형식화 클래스

김정훈·2024년 5월 9일

Java

목록 보기
34/48

형식화 클래스

java.text 패키지 : 형식화 관련 편의 클래스 모음

1. DecimalFormat

숫자 👉 형식화된 문자열
10000 👉 10,000

  • format(...) : 숫자 👉 형식화된 문자열
    0패턴 : 없어도 0출력
    #패턴 : 없으면 제외
public class E {
    public static void main(String[] args) {
        double num1 = 1000000000.123;

        DecimalFormat df = new DecimalFormat("0,000.0000");
        String num1Str = df.format(num1);
        System.out.println(num1Str); //>1,000,000,000.1230

        DecimalFormat df2 = new DecimalFormat("#,###.####");
        num1Str = df2.format(num1);
        System.out.println(num1Str); //>1,000,000,000.123
    }
}
  • Number parse(...) : 형식화된 문자열 👉 숫자

2. SimpleDateFromat

날짜 형식화 : java.util.Date 객체

String format(..) : Date객체 👉 형식화된 문자열

public class Ex03 {
    public static void main(String[] args) {
        Date date= new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd a hh:mm");
        String strDate = sdf.format(date);
        System.out.println(strDate);
    }
}

Date parse(...) : 형식화된 문자열 👉 Date 객체

public class Ex04 {
    public static void main(String[] args) throws ParseException {
        String strDate = "31/05/23 00:00";
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm");

        Date date = sdf.parse(strDate);
        System.out.println(date);
    }
}

3. ChoiceFormat

public class Ex05 {
    public static void main(String[] args) {
        double[] limits = {60,70,80,90};
        String[] grades = {"D","C","B","A"};
        ChoiceFormat cf = new ChoiceFormat(limits, grades);
        int[] scores = {100,70,55,80,95,87};
        for(int score : scores){
            String grade = cf.format(score);
            System.out.printf("점수 : %d, 학점 : %s%n",score, grade);
        }
    }
}

패턴
80#B : 70이상 80미만 - C
80<B : 70이상 80이하 - C
81점이상 90미만 - B

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

        String pattern = "60#D|70#C|80#B|90#A";
        ChoiceFormat cf = new ChoiceFormat(pattern);
        int[] scores = {100,70,55,80,95,87};
        for(int score : scores){
            String grade = cf.format(score);
            System.out.printf("점수 : %d, 학점 : %s%n",score, grade);
        }
    }
}

4. MessageFormat

String format(...) : 형식화된 문자열
Object[] parse(...) : 형식화된 문자열 👉 원래 데이터로 변환

public class Ex06 {
    public static void main(String[] args) {
        String pattern = "이름 : {0}, 전화번호 : {1}";
        String[] names = {"이이름", "김이름", "최이름"};
        String[] mobiles = {"010-0000-0000","010-1000-1000", "010-2000-2000"};
        for(int i =0; i < names.length; i++){
            String str = MessageFormat.format(pattern, names[i], mobiles[i]);
            System.out.println(str);
        }
    }
}
public class Ex07 {
    public static void main(String[] args) throws ParseException {
        String str = "이름 : 이이름, 전화번호 : 010-0000-0000";
        String pattern = "이름 : {0}, 전화번호 : {1}";
        MessageFormat mf = new MessageFormat(pattern);
        Object[] data = mf.parse(str);
        String name = (String)data[0];
        String mobile = (String)data[1];
        System.out.println(name +" "+ mobile);
    }
}
profile
안녕하세요!

0개의 댓글