利用ICU解决日期格式国际化问题
来源:互联网 发布:外贸email软件 编辑:程序博客网 时间:2024/05/22 06:19
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); }
- 利用ICU解决日期格式国际化问题
- 日期格式国际化
- 国际化测试-ICU
- 解决JSON序列化日期格式问题
- web国际化日期显示及数字格式
- oracle查询日期在查询时解决格式问题
- Retrofit 自定义Gson对象解决日期格式问题
- 用Java解决国际化问题
- 用Java解决国际化问题
- native2ascii解决java国际化问题
- 用Java解决国际化问题
- 用Java解决国际化问题
- 用Java解决国际化问题
- 用Java解决国际化问题
- 日期格式转换问题
- Oracle 日期格式问题
- extjs日期格式问题
- oracle日期格式问题
- DataImport(译)
- dedecms取得三级分类及子分类
- glog Notes for Windows users
- java循环
- poj1060
- 利用ICU解决日期格式国际化问题
- Linux下RPM软件包的安装及卸载
- can 驱动头文件
- Discuz! 插件开发实例讲解
- 设计模式 - 迭代器模式
- “鼠标屏幕取词”技术
- 关于final与static
- 解决Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (gbk_chinese_ci,COERCIBLE) for operatio
- 安卓图表引擎AChartEngine