Java中阿拉伯语和常规日期的日期解析问题 [英] Date parsing issue with Arabic and regular date in Java
问题描述
我有一个日期转换器功能,例如:
I have a date converter function like:
public static LocalDate getLocalDateFromString(String dateString) {
DecimalStyle defaultDecimalStyle = DateTimeFormatter.ISO_LOCAL_DATE.getDecimalStyle();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE.withDecimalStyle(defaultDecimalStyle.withZeroDigit('\u0660'));
LocalDate date = LocalDate.parse(dateString, dateTimeFormatter);
return date;
}
它对于诸如 ٢٠١٩-٠٤-١٥
之类的阿拉伯日期也可以正常工作,但是当我传递诸如 2019-07-31
之类的正常日期时,它会抛出异常,因为格式化程序的类型不同:
It works fine for Arabic dates like ٢٠١٩-٠٤-١٥
, but when I pass a normal date like 2019-07-31
, it throws an exception because the formatter is of a different type:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-07-31' could not be parsed at index 0
我无法控制传递的日期,因为它是用户传递的.
I don't have control over the date passed, as it is passed by the user.
如何使用相同的函数来解析两个日期?
How can I use the same function to parse both dates?
推荐答案
了解您的字符串
DateTimeFormatter
对您来说并不容易.我推测这种选择背后可能有一个目的:最好让您自己进入一种情况,即您事先知道要解析的字符串中使用了哪种数字,这会更好.您可能会想到:可以说服字符串源将此信息传递给您吗?
Know your string
DateTimeFormatter
doesn’t make this very easy for you. I am speculating that there may be a purpose behind this choice: it’s better if you can bring yourself into a situation where you know beforehand what kind of digits is used in the string you are going to parse. You may give this a thought: could you persuade the source of your string to pass this information to you?
如果没有,当然有办法解决.以下是低级内容,但应该是一般性内容.
If not, of course there is a way through. The following is low-level but should be general.
public static LocalDate getLocalDateFromString(String dateString) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
// Take a digit from dateString to determine which digits are used
char sampleDigit = dateString.charAt(0);
if (Character.isDigit(sampleDigit)) {
// Subtract the numeric value of the digit to find the zero digit in the same digit block
char zeroDigit = (char) (sampleDigit - Character.getNumericValue(sampleDigit));
assert Character.isDigit(zeroDigit) : zeroDigit;
assert Character.getNumericValue(zeroDigit) == 0 : zeroDigit;
DecimalStyle defaultDecimalStyle = dateFormatter.getDecimalStyle();
dateFormatter = dateFormatter
.withDecimalStyle(defaultDecimalStyle.withZeroDigit(zeroDigit));
}
// If the examined char wasn’t a digit, the following parsing will fail;
// but in that case the best we can give the caller is the exception from that failed parsing.
LocalDate date = LocalDate.parse(dateString, dateFormatter);
return date;
}
让我们尝试一下:
System.out.println("Parsing Arabic date string to "
+ getLocalDateFromString("٢٠١٩-٠٤-١٥"));
System.out.println("Parsing Western date string to "
+ getLocalDateFromString("2019-07-31"));
此代码段的输出为:
Parsing Arabic date string to 2019-04-15
Parsing Western date string to 2019-07-31
这篇关于Java中阿拉伯语和常规日期的日期解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!