java 日期格式化SimpleDateFormat Local西班牙等国家的日期格式化
来源:互联网 发布:团购的软件 编辑:程序博客网 时间:2024/05/18 03:41
今天在开发的过程中遇到了多语言日期格式化的问题,所有在此记录下,以便下次备忘。
java.text.SimpleDateFormat.SimpleDateFormat(String pattern, Locale locale) 中的Locale只给我们提供了部分国家作为常量来供我们调用,当我们遇到像西班牙这样它并没有提供给我们常量的国家时,我们应该怎么样来对其日期进行格式化呢?
之前百度了都没找到方案,所有就去stackoverflow里参考了下国外网友的想法,接下来上代码:
<span style="font-size:18px;">/* * 根据国家对相应的时间进行日期格式化 */public static Date dateFormate(String dateStr, int countryid) throws ParseException {Countries countries = countriesMapper.selectByPrimaryKey(countryid);SimpleDateFormat format = null;if ("DE".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd. MMMM yyyy", Locale.GERMAN);} else if ("GB".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);} else if ("IT".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd MMMM yyyy", Locale.ITALY);dateStr = dateStr.toUpperCase();} else if ("FR".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd MMMM yyyy", Locale.FRANCE);dateStr = dateStr.toUpperCase();} else if ("ES".equals(countries.getAlpha2())) {format = new SimpleDateFormat("d 'de' MMMM 'de' yyyy", new Locale("es", "ES"));} else if ("JP".equals(countries.getAlpha2())) {format = new SimpleDateFormat("yyyy'年'MM'月'dd'日'", Locale.JAPAN);}else if ("US".equals(countries.getAlpha2())) {format = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);}else if ("CA".equals(countries.getAlpha2())) {format = new SimpleDateFormat("MMMM dd, yyyy", Locale.CANADA);}return format.parse(dateStr);}</span>
对于Locale没有提供常量的国家我们可以通过创建Locale对象的方式实现:<span style="font-size:18px;">format = new SimpleDateFormat("d 'de' MMMM 'de' yyyy", new Locale("es", "ES"));</span>
以上是我的总结,欢迎大家补充,谢谢。
<span style="font-size:18px;">/* * 根据国家对相应的时间进行日期格式化 */public static Date dateFormate(String dateStr, int countryid) throws ParseException {Countries countries = countriesMapper.selectByPrimaryKey(countryid);SimpleDateFormat format = null;if ("DE".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd. MMMM yyyy", Locale.GERMAN);} else if ("GB".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);} else if ("IT".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd MMMM yyyy", Locale.ITALY);dateStr = dateStr.toUpperCase();} else if ("FR".equals(countries.getAlpha2())) {format = new SimpleDateFormat("dd MMMM yyyy", Locale.FRANCE);dateStr = dateStr.toUpperCase();} else if ("ES".equals(countries.getAlpha2())) {format = new SimpleDateFormat("d 'de' MMMM 'de' yyyy", new Locale("es", "ES"));} else if ("JP".equals(countries.getAlpha2())) {format = new SimpleDateFormat("yyyy'年'MM'月'dd'日'", Locale.JAPAN);}else if ("US".equals(countries.getAlpha2())) {format = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);}else if ("CA".equals(countries.getAlpha2())) {format = new SimpleDateFormat("MMMM dd, yyyy", Locale.CANADA);}return format.parse(dateStr);}</span>
<span style="font-size:18px;">format = new SimpleDateFormat("d 'de' MMMM 'de' yyyy", new Locale("es", "ES"));</span>
0 0
- java 日期格式化SimpleDateFormat Local西班牙等国家的日期格式化
- java SimpleDateFormat格式化日期
- java 日期格式化-- SimpleDateFormat 的使用
- Java日期格式化类SimpleDateFormat
- java综合:SimpleDateFormat格式化日期
- java SimpleDateFormat格式化输出日期
- Java SimpleDateFormat进行日期格式化
- 日期格式化类SimpleDateFormat
- SimpleDateFormat格式化日期
- SimpleDateFormat格式化日期
- SimpleDateFormat格式化日期
- SimpleDateFormat格式化日期
- SimpleDateFormat格式化日期
- SimpleDateFormat格式化日期
- SimpleDateFormat格式化日期
- SimpleDateFormat格式化日期
- SimpleDateFormat 日期格式化
- SimpleDateFormat格式化日期
- 升级SQLite数据库
- Java:protected提供了包内访问权限
- MindManager中的摘要样式该如何进行运用
- tensorflow学习笔记(十八):Multiple GPUs
- 分布式事务的处理这个讲得比较透彻了
- java 日期格式化SimpleDateFormat Local西班牙等国家的日期格式化
- c中的“:”
- openwrt添加usb storage支持
- WebStorm快捷键操作
- iOS 下APNS推送处理函数详解
- swift 搜索框(UISearchBar)实现简单的搜索功能(swift3)
- Android studio 使用Grandle进行分渠道打包
- spring-mvc整合jquery cropper图片裁剪插件
- C语言直接对数据加密解密