利用ICU解决日期格式国际化问题
2013-06-07 16:21
323 查看
ICU jar包下载地址:http://apps.icu-project.org/icu-jsp/downloadPage.jsp?ver=51.2&base=j&svn=release-51-2
日期格式间的相互转换:
日期格式转换为各个国家的日期格式时主要使用了format函数(最终调用了Calendar的setTime函数):
日期格式间的相互转换:
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); }
相关文章推荐
- 解决Struts2 json-plugin Date或Timestamp等日期格式带T的问题
- 解决oracle取日期到页面为英文格式的问题
- 解决asp.net core 日期格式 datetime Json返回 带T的问题
- 解决json日期格式问题的3种方法
- Gregarius中文日期格式问题解决办法
- SpringMVC(28):json数据的时间日期格式问题-解决与示例
- 解决Struts2 json-plugin Date或Timestamp等日期格式带T的问题
- 使用newtonjson解决Json日期格式问题
- Jox(解决了日期格式转化和导出XML中文乱码问题)
- 4种解决json日期格式问题的办法
- 通过自己定义MVC的Controller的Json转换器解决日期序列化格式问题
- springmvc学习笔记-返回json的日期格式问题的解决方法
- 4种解决json日期格式问题的办法
- struts2在Linux CentOS下日期格式不正常的问题的解决方法
- 使用poi实现java读取excel表格并存储到数据库中解决读取日期格式问题(ssm框架)
- Json日期格式问题的四种解决方法(超详细)
- 4种解决json日期格式问题的办法
- 解决json日期格式问题的3种方法
- 解决json日期格式问题的3种方法(转载)
- 土办法解决日期转换问题,转出格式为。。。 贰零壹陆年零壹月零捌日