5。Locale 클래스

Locale 객체는 특정 지리적, 정치적, 문화적 영역을 나타내며, 사용자의 지역 정보를 기반으로 시스템 설정을 조정하는 역할을 합니다.

주요 생성자

LocaleExample 예제 코드

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

public class LocaleExample {
    static public void localizedFormat(String pattern, double value, Locale loc) {
        NumberFormat nf = NumberFormat.getNumberInstance(loc);
        DecimalFormat df = (DecimalFormat) nf;
        df.applyPattern(pattern);
        String output = df.format(value);
        System.out.println(pattern + "\\t" + output + "\\t" + loc.toString());
    }

    public static void main(String[] args) {
        Locale locale = new Locale("ko", "KR");
        System.out.println(locale.getLanguage());
        System.out.println(locale.getCountry());
        System.out.println(locale.getDisplayCountry());

        Locale locale2 = new Locale("en", "US");
        System.out.println(locale2.getLanguage());
        System.out.println(locale2.getCountry());
        System.out.println(locale2.getDisplayCountry());

        Locale[] locales = {
            new Locale("en", "US"),
            new Locale("de", "DE"),
            new Locale("fr", "FR"),
            new Locale("ko", "KR")
        };

        for (int i = 0; i < locales.length; i++) {
            localizedFormat("###,###.###", 123456.789, locales[i]);
        }
    }
}


TimeZone 클래스

TimeZone 객체는 특정 시간대의 오프셋을 나타내며, 시스템의 현재 시간대를 조정하는 역할을 합니다.

주요 메서드

TimezoneExample 예제 코드

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimezoneExample {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println((calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000));

        TimeZone tz = TimeZone.getDefault();
        System.out.println("Timezone offset: " + tz.getID());
        System.out.println("Timezone name: " + tz.getDisplayName());
        System.out.println("Timezone ID: " + (tz.getRawOffset() / 1000 / 60) + "분");
        System.out.println("Summer Time 사용 여부: " + tz.useDaylightTime());

        Date date = new Date();
        System.out.println(date);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss (z Z)");
        System.out.format("%s%n%s%n%n", tz.getDisplayName(), df.format(date));

        TimeZone tz2 = TimeZone.getTimeZone("Asia/Seoul");
        df.setTimeZone(tz2);
        System.out.format("%s%n%s%n%n", tz2.getDisplayName(), df.format(date));

        TimeZone tz3 = TimeZone.getTimeZone("America/Los_Angeles");
        df.setTimeZone(tz3);
        System.out.format("%s%n%s%n%n", tz3.getDisplayName(), df.format(date));
    }
}


6。형식화 클래스

DecimalFormat 클래스

DecimalFormat 클래스는 숫자를 특정 형식에 맞춰 변환하는 기능을 제공합니다.