我不会说俄语,所以我在验证月份拼写是否正确等方面遇到了困难.老实说,我不完全确定我的输入是俄语(俄语是谷歌翻译检测到的语言)
我用Kotlin编写了一些代码,它会尽最大努力解析以各种格式和语言指定的日期.然而,我在分析俄罗斯日期方面遇到了困难.以下是我的代码的相关部分:
sequenceOf(
"ru-RU", // Russian
"sr", // Serbian
).forEach {
val format = DateTimeFormatter.ofPattern("d MMM. yyyy")
.withLocale(Locale.forLanguageTag(it))
try {
return listOf(LocalDate.parse(dateString, format))
} catch (e: Exception) {
//Ignore and move on
}
}
此代码可以正确解析"27 апр. 2018"
和"24 мая. 2013"
,但无法解析"28 фев. 2019"
.
"28 фев. 2019"
有什么特别之处和/或如何正确解析此值?
如果您用Java提供答案,我可以很容易地将其翻译成Kotlin.
EDIT:为Kotlin 的SSCCE干杯:
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.*
println("System.getProperty - " + System.getProperty("java.version"));
println("Runtime.version - " + Runtime.version());
val dateString = "28 фев. 2019"
sequenceOf(
"ru-RU", // Russian
"sr", // Serbian
).forEach {
val format = DateTimeFormatter.ofPattern("d MMM. yyyy")
.withLocale(Locale.forLanguageTag(it))
try {
println("Parse successful - " + LocalDate.parse(dateString, format))
} catch (e: Exception) {
println("Parse failed - " + e)
}
}
我的系统上的输出:
System.getProperty - 17.0.4.1
Runtime.version - 17.0.4.1+7-b469.62
Parse failed - java.time.format.DateTimeParseException: Text '28 фев. 2019' could not be parsed at index 3
Parse failed - java.time.format.DateTimeParseException: Text '28 фев. 2019' could not be parsed at index 3