如何实现英文数字与阿拉伯语月份混合的日期格式输出?
解决方案
你要实现「年份 + 阿拉伯语月份」的显示格式,只需调整两处配置即可:
- 修正日期模式串:你当前使用的
d MMM yyyy是「日 月份 年」的顺序,替换为yyyy MMM即可匹配年在前、月份在后的需求 - 确保传入的
locale参数为阿拉伯语区域,否则会输出对应区域语言的月份名称
修正后的SimpleDateFormat示例代码
// 可根据需求指定具体阿拉伯国家区域,比如ar-SA(沙特)、ar-EG(埃及)等 Locale arabicLocale = Locale.forLanguageTag("ar"); SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy MMM", arabicLocale); dateString = dateFormatter.format(date);
补充说明
- 如果需要显示完整的月份名称而非缩写,把模式串中的
MMM替换为MMMM即可 - Java 8及以上版本更推荐使用线程安全的
java.timeAPI实现,示例如下:
Locale arabicLocale = Locale.forLanguageTag("ar"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MMM", arabicLocale); // 替换为你自己的日期对象即可 String dateString = LocalDate.now().format(formatter);
内容的提问来源于stack exchange,提问作者Ahsan Malik
相关产品推荐
相关产品推荐

