利用ICU解決日期格式國際化問題

ICU jar包下載地址:http://apps.icu-project.org/icu-jsp/downloadPage.jsp?ver=51.2&base=j&svn=release-51-2

日期格式間的相互轉換:

import java.text.FieldPosition;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


import com.ibm.icu.text.DateFormat;


public class dataformat {
    public void run() {


        // Formatting Dates


        DateFormat dfUS = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
        DateFormat dfFrance = DateFormat.getDateInstance(DateFormat.FULL, Locale.FRANCE);
        Calendar c = Calendar.getInstance();
        Date d = c.getTime();
        
        StringBuffer sb = new StringBuffer();
        sb = dfUS.format(d, sb, new FieldPosition(0));
        System.out.println(sb.toString());


        StringBuffer sbf = new StringBuffer();
        sbf = dfFrance.format(d, sbf, new FieldPosition(0));
        System.out.println(sbf.toString());


        StringBuffer sbg = new StringBuffer();
        DateFormat dfg = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT);
        FieldPosition pos = new FieldPosition(DateFormat.MINUTE_FIELD);
        sbg = dfg.format(d, sbg, pos);
        System.out.println(sbg.toString());
        System.out.println(sbg.toString().substring(pos.getBeginIndex(), pos.getEndIndex()));


        // Parsing Dates


        String dateString_US = "Thursday, February 7, 2008";
        String dateString_FRANCE = "jeudi 7 février 2008";
        try {
            Date parsedDate_US = dfUS.parse(dateString_US);
            Date parsedDate_FRANCE = dfFrance.parse(dateString_FRANCE);
            System.out.println(parsedDate_US.toString());
            System.out.println(parsedDate_FRANCE.toString());
        } catch (ParseException pe) {
            System.out.println("Exception while parsing :" + pe);
        }
    }


    public static void main(String args[]) {
        new dataformat().run();
    }
}


日期格式轉換爲各個國家的日期格式時主要使用了format函數(最終調用了Calendar的setTime函數):

    /**
     * Formats a time object into a time string. Examples of time objects
     * are a time value expressed in milliseconds and a Date object.
     * @param obj must be a Number or a Date or a Calendar.
     * @param toAppendTo the string buffer for the returning time string.
     * @return the formatted time string.
     * @param fieldPosition keeps track of the position of the field
     * within the returned string.
     * On input: an alignment field,
     * if desired. On output: the offsets of the alignment field. For
     * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
     * if the given fieldPosition is DateFormat.YEAR_FIELD, the
     * begin index and end index of fieldPosition will be set to
     * 0 and 4, respectively.
     * Notice that if the same time field appears
     * more than once in a pattern, the fieldPosition will be set for the first
     * occurrence of that time field. For instance, formatting a Date to
     * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
     * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
     * the begin index and end index of fieldPosition will be set to
     * 5 and 8, respectively, for the first occurrence of the timezone
     * pattern character 'z'.
     * @see java.text.Format
     * @stable ICU 2.0
     */
    public final StringBuffer format(Object obj, StringBuffer toAppendTo,
                                     FieldPosition fieldPosition)
    {
        if (obj instanceof Calendar)
            return format( (Calendar)obj, toAppendTo, fieldPosition );
        else if (obj instanceof Date)
            return format( (Date)obj, toAppendTo, fieldPosition );
        else if (obj instanceof Number)
            return format( new Date(((Number)obj).longValue()),
                          toAppendTo, fieldPosition );
        else
            throw new IllegalArgumentException("Cannot format given Object (" +
                                               obj.getClass().getName() + ") as a Date");
    }

    public StringBuffer format(Date date, StringBuffer toAppendTo,
                                     FieldPosition fieldPosition) {
        // Use our Calendar object
        calendar.setTime(date);
        return format(calendar, toAppendTo, fieldPosition);
    }


發佈了37 篇原創文章 · 獲贊 4 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章