如何将日期从英语转换为阿拉伯语
How to convert date from English to Arabic
我有这个代码
frame.sigdate.setText(new SimpleDateFormat("yyyy/M/d").format(new Date()));
它从我的电脑上读取带有英文数字的日期。我想要做的是将日期转换为阿拉伯数字。
有没有像 Local.ar
这样的东西?
感谢您的帮助。
试试下面的方法
java.util.Locale locale = new java.util.Locale("ar");
java.text.DecimalFormat df = (java.text.DecimalFormat)
java.text.DecimalFormat.getNumberInstance(locale);
DateTime dateTimeObjectInUTC = new DateTime(DateTimeZone.UTC);
DateTimeZone dateTimeZoneObject = DateTimeZone.forID("Asia/Riyadh");
java.util.Locale locale = new Locale("ar","SA");
DateTimeFormatter formatter = DateTimeFormat.forStyle("FF").withLocale(locale).withZone(dateTimeZoneObject);
String output = formatter.print(dateTimeObjectInUTC);
这应该有所帮助!
我正在使用 Joda-Time。请参考 Jodatime 文档。 DateTimeZone
documentation,例如
java.time
Locale arabicLocale = Locale.forLanguageTag("ar");
DateTimeFormatter arabicDateFormatter
= DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(arabicLocale)
.withDecimalStyle(DecimalStyle.of(arabicLocale));
LocalDate today = LocalDate.now(ZoneId.of("Asia/Muscat"));
System.out.println(today.format(arabicDateFormatter));
输出:
١٥/٤/٢٠١٨
关键是withDecimalStyle
。如果没有这个调用,格式化程序仍将使用西方数字,如 15/4/2018
。您可能希望使用更具体的语言标签,而不仅仅是 ar
用于阿拉伯语,例如 ar-BH
用于巴林或 ar-YE
用于也门。请参阅底部的 link 了解可能性。您还应该在我放置 Asia/Muscat.
的位置插入您想要的时区
编辑: 以上已经在 Java 9 中进行了测试。令人惊讶的是,在 Java 8 中它仍然使用西方(非本地化)数字。一个可能的解决方法(或解决方法,如果你喜欢的话)是明确指定零数字——它会从中获取其他数字。
DecimalStyle arabicDecimalStyle
= DecimalStyle.of(arabicLocale).withZeroDigit('٠');
DateTimeFormatter arabicDateFormatter
= DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(arabicLocale)
.withDecimalStyle(arabicDecimalStyle);
withZeroDigit
的参数中两个撇号之间的阿拉伯数字零。现在我在 Java 8:
上得到这个输出
١٥/٠٤/١٨
使用内置的语言环境特定格式通常是个好主意,就像我在上面的两个片段中使用 ofLocalizedDate
一样。如果您需要更好地控制格式,请改用 ofPattern
。例如,要获取 yyyy/mm/dd 格式:
DateTimeFormatter arabicDateFormatter
= DateTimeFormatter.ofPattern("uuuu/MM/dd", arabicLocale)
.withDecimalStyle(arabicDecimalStyle);
输出:
٢٠١٨/٠٤/١٥
格式从 Java 8 更改为 Java 9 的原因是 Java 更改了区域设置数据来源的默认值,包括内置的本地化日期和时间格式。您可以通过设置系统 属性 来获得 Java 8 中已有的 Java 9 格式,例如:
System.setProperty("java.locale.providers", "CLDR,JRE,SPI");
通过此更改,上面的第一个代码片段在 Java 8 上的输出与 Java 9 上的输出相同:
١٥/٤/٢٠١٨
这里的重要细节是 CLDR
在 属性 字符串中排在第一位。优点是您不需要指定自己的格式模式字符串,本地化到其他语言环境很简单,一旦您切换到 Java 9 或更高版本,用户不会对行为变化感到惊讶。
我正在使用并推荐 java.time
,现代 Java 日期和时间 API。您在问题中使用的 SimpleDateFormat
class 不仅早已过时,而且出了名的麻烦。恕我直言,你应该完全避免它。现代 API 更好用。
链接
- Oracle tutorial: Date Time 解释如何使用
java.time
。
- List of supported locales in Java 8
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
String date = "16/08/2011";
Locale arabicLocale = Locale.forLanguageTag("ar-SA");
DateTimeFormatter arabicDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(arabicLocale).withDecimalStyle(DecimalStyle.of(arabicLocale));
LocalDate today = LocalDate.now(ZoneId.of("Asia/Muscat"));
today = LocalDate.parse(date, formatter);
String dat = today.format(arabicDateFormatter);
System.out.println(dat);
输出 ١٦ /٨ /٢٠١١
我有这个代码
frame.sigdate.setText(new SimpleDateFormat("yyyy/M/d").format(new Date()));
它从我的电脑上读取带有英文数字的日期。我想要做的是将日期转换为阿拉伯数字。
有没有像 Local.ar
这样的东西?
感谢您的帮助。
试试下面的方法
java.util.Locale locale = new java.util.Locale("ar");
java.text.DecimalFormat df = (java.text.DecimalFormat)
java.text.DecimalFormat.getNumberInstance(locale);
DateTime dateTimeObjectInUTC = new DateTime(DateTimeZone.UTC);
DateTimeZone dateTimeZoneObject = DateTimeZone.forID("Asia/Riyadh");
java.util.Locale locale = new Locale("ar","SA");
DateTimeFormatter formatter = DateTimeFormat.forStyle("FF").withLocale(locale).withZone(dateTimeZoneObject);
String output = formatter.print(dateTimeObjectInUTC);
这应该有所帮助!
我正在使用 Joda-Time。请参考 Jodatime 文档。 DateTimeZone
documentation,例如
java.time
Locale arabicLocale = Locale.forLanguageTag("ar");
DateTimeFormatter arabicDateFormatter
= DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(arabicLocale)
.withDecimalStyle(DecimalStyle.of(arabicLocale));
LocalDate today = LocalDate.now(ZoneId.of("Asia/Muscat"));
System.out.println(today.format(arabicDateFormatter));
输出:
١٥/٤/٢٠١٨
关键是withDecimalStyle
。如果没有这个调用,格式化程序仍将使用西方数字,如 15/4/2018
。您可能希望使用更具体的语言标签,而不仅仅是 ar
用于阿拉伯语,例如 ar-BH
用于巴林或 ar-YE
用于也门。请参阅底部的 link 了解可能性。您还应该在我放置 Asia/Muscat.
编辑: 以上已经在 Java 9 中进行了测试。令人惊讶的是,在 Java 8 中它仍然使用西方(非本地化)数字。一个可能的解决方法(或解决方法,如果你喜欢的话)是明确指定零数字——它会从中获取其他数字。
DecimalStyle arabicDecimalStyle
= DecimalStyle.of(arabicLocale).withZeroDigit('٠');
DateTimeFormatter arabicDateFormatter
= DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(arabicLocale)
.withDecimalStyle(arabicDecimalStyle);
withZeroDigit
的参数中两个撇号之间的阿拉伯数字零。现在我在 Java 8:
١٥/٠٤/١٨
使用内置的语言环境特定格式通常是个好主意,就像我在上面的两个片段中使用 ofLocalizedDate
一样。如果您需要更好地控制格式,请改用 ofPattern
。例如,要获取 yyyy/mm/dd 格式:
DateTimeFormatter arabicDateFormatter
= DateTimeFormatter.ofPattern("uuuu/MM/dd", arabicLocale)
.withDecimalStyle(arabicDecimalStyle);
输出:
٢٠١٨/٠٤/١٥
格式从 Java 8 更改为 Java 9 的原因是 Java 更改了区域设置数据来源的默认值,包括内置的本地化日期和时间格式。您可以通过设置系统 属性 来获得 Java 8 中已有的 Java 9 格式,例如:
System.setProperty("java.locale.providers", "CLDR,JRE,SPI");
通过此更改,上面的第一个代码片段在 Java 8 上的输出与 Java 9 上的输出相同:
١٥/٤/٢٠١٨
这里的重要细节是 CLDR
在 属性 字符串中排在第一位。优点是您不需要指定自己的格式模式字符串,本地化到其他语言环境很简单,一旦您切换到 Java 9 或更高版本,用户不会对行为变化感到惊讶。
我正在使用并推荐 java.time
,现代 Java 日期和时间 API。您在问题中使用的 SimpleDateFormat
class 不仅早已过时,而且出了名的麻烦。恕我直言,你应该完全避免它。现代 API 更好用。
链接
- Oracle tutorial: Date Time 解释如何使用
java.time
。 - List of supported locales in Java 8
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/yyyy");
String date = "16/08/2011";
Locale arabicLocale = Locale.forLanguageTag("ar-SA");
DateTimeFormatter arabicDateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(arabicLocale).withDecimalStyle(DecimalStyle.of(arabicLocale));
LocalDate today = LocalDate.now(ZoneId.of("Asia/Muscat"));
today = LocalDate.parse(date, formatter);
String dat = today.format(arabicDateFormatter);
System.out.println(dat);
输出 ١٦ /٨ /٢٠١١