我工作的阿拉伯文网络监控项目,我想字符串转换日期像这样的:如何将阿拉伯字符串日期转换为java 8日期对象?
الاثنين 24 أبريل 2017 - 15:00
到Java 8日期对象。我怎样才能做到这一点?
我工作的阿拉伯文网络监控项目,我想字符串转换日期像这样的:如何将阿拉伯字符串日期转换为java 8日期对象?
الاثنين 24 أبريل 2017 - 15:00
到Java 8日期对象。我怎样才能做到这一点?
一种解决方案可能是翻译的日期为英语和解析它,然后:
private final static Map<String, Integer> monthMapping = new HashMap<>();
static {
// list of all month.
monthMapping.put("أبريل", "4");
}
public Date fromArabicToDate(String arabicInput) throws ParseException {
String[] parts = arabicInput.split(" ");
if (parts.length != 4)
throw new IllegalArgumentException();
String dateInput = parts[0] + "-" + monthMapping.get(parts[1]) + "-" + parts[2];
SimpleDateFormat parser = new SimpleDateFormat("YYYY-MM-DD");
return parser.parse(dateInput);
}
我试过一个月来复制,但我不相信我已经正确的。解析时切换put
的参数。你可以看看Joda-Time。也许他们有一个解决方案。这是mentioned here。
哦......过时的信息。你永远不要停止学习。谢谢@Hulk。这是我心中的某个地方。我发现它在相同的地方,但它已超过4年:http://stackoverflow.com/a/14439397/2097191(我改变了我的答案) – Obenland
你所指的答案似乎是关于[SimpleDateFormat'的一个特定构造函数](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html#SimpleDateFormat-java.lang.String-java.util.Locale- ):“使用给定的模式和默认的FORMAT语言环境的默认日期格式符号构造一个SimpleDateFormat注意:此构造函数可能不支持所有语言环境,为了全面覆盖,请使用DateFormat类中的工厂方法。 – Hulk
我不知道阿拉伯语足够了解阿拉伯语格式的日期。然而这段代码:
Locale arabicLocale = new Locale.Builder().setLanguageTag("ar-SA-u-nu-arab").build();
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(arabicLocale);
String formatted = date.format(formatter);
System.out.println(formatted);
System.out.println(formatter.parse(formatted));
息率这样的输出:
26 أبريل, 2017
{},ISO resolved to 2017-04-26
创建Locale
是一个答案Setting Arabic numbering system locale doesn't show Arabic numbers
您可以定义自己的FormatStyle
微调此格式的代码。
编辑:以感谢苗条和梅诺Hochschild上寻找灵感:
String dateTimeString = "الاثنين 24 أبريل 2017 - 15:00";
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("EEEE d MMMM uuuu - HH:mm", new Locale("ar"));
LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
System.out.println(dateTime);
此打印:
2017-04-24T15:00
只需构造不带nu扩展名的语言环境。它没有工作,并应该失败,你的nu延伸(但不),看到我的答案。 –
非常感谢,@MenoHochschild。在我的电脑上,即使是'新的Locale(“ar”)'也能工作,所以我把它放在了复杂的语言标记中。 –
解析字符串时,假定日期需要,您必须指定字符集解析将始终以您提供的格式工作:
public static Date getDate(String strDate) throws Exception{
strDate=new String(strDate.getBytes(),"UTF-8");
Map<String, Integer> months = new HashMap<>();
String JAN = new String("يناير".getBytes(), "UTF-8");
String FEB = new String("فبراير".getBytes(), "UTF-8");
String MAR = new String("مارس".getBytes(), "UTF-8");
String APR = new String("أبريل".getBytes(), "UTF-8");
String APR_bis = new String("ابريل".getBytes(), "UTF-8");
String MAY = new String("ماي".getBytes(), "UTF-8");
String JUN = new String("بونيو".getBytes(), "UTF-8");
String JUN_bis = new String("يونيه".getBytes(), "UTF-8");
String JUL = new String("يوليوز".getBytes(), "UTF-8");
String AUG = new String("غشت".getBytes(), "UTF-8");
String SEP = new String("شتنبر".getBytes(), "UTF-8");
String SEP_bis = new String("سبتمبر".getBytes(), "UTF-8");
String OCT = new String("أكتوبر".getBytes(), "UTF-8");
String OCT_bis = new String("اكتوبر".getBytes(), "UTF-8");
String NOV = new String("نونبر".getBytes(), "UTF-8");
String NOV_bis = new String("نوفمبر".getBytes(), "UTF-8");
String DEC = new String("دجنبر".getBytes(), "UTF-8");
String DEC_bis = new String("ديسمبر".getBytes(), "UTF-8");
months.put(JAN, 0);
months.put(FEB, 1);
months.put(MAR, 2);
months.put(APR, 3);
months.put(APR_bis, 3);
months.put(MAY, 4);
months.put(JUN, 5);
months.put(JUN_bis, 5);
months.put(JUL, 6);
months.put(AUG, 7);
months.put(SEP, 8);
months.put(SEP_bis, 8);
months.put(OCT, 9);
months.put(OCT_bis, 9);
months.put(NOV, 10);
months.put(NOV_bis, 10);
months.put(DEC, 11);
months.put(DEC_bis, 11);
StringTokenizer stringTokenizer = new StringTokenizer(strDate);
Calendar calendar = Calendar.getInstance();
while(stringTokenizer.hasMoreElements()) {
stringTokenizer.nextElement();// to skip the first string which is the name of the day
int day = Integer.parseInt(stringTokenizer.nextElement().toString().trim());
String strMonth = stringTokenizer.nextElement().toString().trim();
int month = months.get(strMonth);
int year = Integer.parseInt(stringTokenizer.nextElement().toString().trim());
calendar.set(year, month, day);
}
return calendar.getTime();
}
它给出了这个o输出:
Fri Oct 20 15:26:47 WEST 2017
我错过了您的字符串转换中的一点。在我的计算机上'new String(“يناير”。getBytes(),“UTF-8”)'产生与'يناير''相同的字符串。所有其他月份名称都一样。在具有不同默认字符集的计算机上,结果可能会有所不同。 –
是啊@ OleV.V。它被添加来指定想要的字符集是'UTF-8' –
@Ole和@slim的答案正在工作,但不是因为他们认为的原因。
首先观察 - 怒江扩展是不需要给出的例子:
OLES建议也将工作的语言环境new Locale("ar", "SA")
,而不是Locale.forLanguageTag("ar-SA-u-nu-arab")
。那么这里的unicode-nu-extension是什么呢?没有。下一个问题:
nu-extension应该在这里做什么?
nu-code-word-word“alla”是specified by the unicode consortium以产生阿拉伯数字。但是要解析的输入只有西方数字0-9(历史上它们被阿拉伯人取代,并被指定为代码字“latn” - 顺便提一句,用法不当)。因此,如果NU-扩展已经真的在这里完成了它的任务,然后解析竟没因为arabic-indic digits不是0-9而是:
显然,Java-8中的新时间API不支持nu-extension。
SimpleDateFormat
是否支持nu-extension?
使用下面的代码的调试,我发现,NU-扩展仅支持泰国的数字(见java.util.Locale
类的官方的javadoc而不是阿拉伯语,印度语数字:
SimpleDateFormat sdf =
new SimpleDateFormat("EEEE d MMMM yyyy - HH:mm", Locale.forLanguageTag("ar-SA-nu-arab"));
Date d = sdf.parse(dateTimeString);
System.out.println(d);
String formatted = sdf.format(d);
System.out.println(formatted);
System.out.println(sdf.format(d).equals(dateTimeString));
sdf = new SimpleDateFormat("EEEE d MMMM uuuu - HH:mm", Locale.forLanguageTag("ar-SA-u-nu-thai"));
String thai = sdf.format(d);
System.out.println("u-nu-thai: " + thai);
我承担类DateTimeFormatter
的Java-8还支持泰文数字
结论:
忘记NU-的扩展名常量通过老式的方式来扩展语言环境,而不需要unicode扩展,并采用这种方式来调整Oles的答案。它的工作原理是因为你的输入只有西方数字0-9。
对于包括用于各种编号系统的nu-extension(如果有这样的输入)在内的广泛i18n支持,您可以考虑外部库(例如ICU4J或我的lib Time4J)。
您是否尝试过使用语言环境来使用阿拉伯语的国家?请参阅:http://stackoverflow.com/questions/29154887/setting-arabic-numbering-system-locale-doesnt-show-arabic-numbers/29155743#29155743 – slim
这是不是已经解决了34搜索时已发现[搜索“java arabic date”](http://stackoverflow.com/search?q=java+arabic+date)? –