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);
}