I don't speak Russian, so I'm having trouble validating whether the months are correctly spelled, etc. To be honest, I'm not fully sure that my input is in Russian (Russian is the language detected by Google translate)
I have some code in Kotlin which does a best-effort to parse dates specified in various formats and languages. I'm struggling with parsing Russian dates, however. Here's the relevant part of my code:
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
}
}
This code correctly parses "27 апр. 2018"
and "24 мая. 2013"
, but fails on "28 фев. 2019"
.
What's special about "28 фев. 2019"
and/or how can I parse this value correctly?
If you provide answers in Java, I can translate it to Kotlin fairly easily.
EDIT: Here's an SSCCE in Kotlin:
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)
}
}
Output on my system:
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