Reputation: 47
This Java code:
LocalDate.parse("12 Сен 2018", DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(new Locale("ru", "RUS")).ofPattern("dd MMM yyyy"));
leads to the
java.time.format.DateTimeParseException: Text '12 Сен 2018' could not be parsed at index 3
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
Tried all of Proper Russian month string translation Java examples - no result.
What's wrong with this code ?
Note: I'm using Java 8
Upd: Tried this on local machine and in online compiler - in online compiler works in local machine - came error.
Map<Long, String> map = new HashMap<>();
map.put(1L, "Янв");
map.put(2L, "Фев");
map.put(3L, "Мар");
map.put(4L, "Апр");
map.put(5L, "Май");
map.put(6L, "Июн");
map.put(7L, "Июл");
map.put(8L, "Авг");
map.put(9L, "Cен");
map.put(10L, "Окт");
map.put(11L, "Ноя");
map.put(12L, "Дек");
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("dd ")
.appendText(ChronoField.MONTH_OF_YEAR, map)
.appendPattern(" yyyy")
.toFormatter(new Locale("ru", "RU"));
System.out.println(LocalDate.parse("12 Cен 2018", formatter));
Upvotes: 2
Views: 238
Reputation: 45339
There are two problems:
First, ofPattern
simply creates a pattern-based formatter, therefore discarding the locale:
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(new Locale("ru", "RUS"))
.ofPattern("dd MMM yyyy") // <------ static method!!
ofPattern
is a static method that simply creates a new DateTimeFormatter
instance (see below for a correct way to build it)
Second: the short month name seems incorrect for the pattern:
DateTimeFormatter.ofPattern("dd MMM yyyy")
.withLocale(new Locale("ru", "RUS"))
.format(LocalDate.of(2018, 9, 12))
Results in "12 сент. 2018"
. Java expects сент.
, not сен
.
This works and returns 2018-09-12
:
LocalDate.parse("12 сент. 2018",
DateTimeFormatter.ofPattern("dd MMM yyyy")
.withLocale(new Locale("ru", "RUS")))
Upvotes: 1
Reputation: 11
Hi use this piece of Code :
Locale locale = new Locale("ru", "RU");
String pattern = "dd MMM yyyy";
String date = "03 сен 2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, locale);
LocalDate localDate = LocalDate.parse(date, formatter);
System.out.println(localDate);
Upvotes: 1
Reputation: 121088
Well you need to create a custom formatter for this, which is not that complicated (first time knowing russian, become handy answering a stackoverflow question):
Map<Long, String> map = new HashMap<>();
map.put(1L, "янв");
map.put(2L, "фев");
map.put(3L, "мар");
map.put(4L, "апр");
map.put(5L, "май");
map.put(6L, "июн");
map.put(7L, "июл");
map.put(8L, "авг");
map.put(9L, "сен");
map.put(10L, "окт");
map.put(11L, "ноя");
map.put(12L, "дек");
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("dd ")
.appendText(ChronoField.MONTH_OF_YEAR, map)
.appendPattern(" yyyy")
.toFormatter(new Locale("ru"));
System.out.println(LocalDate.parse("12 сен 2018", fmt)); // 2018-09-12
Upvotes: 2