1

I'm trying to localize for Finland using this code:

Locale finLocale = new Locale("fi", "FI");
Date today = new Date(2017, 1, 1);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, finLocale);
String formattedDate = dateFormat.format(today);
System.out.println(formattedDate);

What I end up getting is "helmikuutata". I would expect "helmikuu" or "helmikuuta", but this just seems wrong.

Is this valid Finnish, or is there a bug in Java? My version is 1.8.0_31

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Gary
  • 78
  • 5
  • 4
    I'm getting `1. helmikuuta 3917`. – shmosel Jun 29 '17 at 00:29
  • 1
    As an aside, everyone should avoid the outdated classes `Date` and `DateFormat` and even more so the deprecated 3-arg `Date()` constructor. And when you are using Java 8, there are no excuses left. The Java 8 date and time API is much more programmer friendly and nice to work with, so do yourself the favour. It has been backported to Java 6 and 7 too. – Ole V.V. Jun 30 '17 at 22:42

2 Answers2

5

Yes, this was a bug in JDK (See JDK-8074791), wherein an extra 'ta' was appended to the month name. This got fixed from JDK 8u60 version onwards. So, if you upgrade to latest JDK versions like JDK8u131, you will get the correct output.

Pallavi Sonal
  • 3,661
  • 1
  • 15
  • 19
3

I am convinced that the answer by Pallavi Sonal is correct. I have already upvoted it and you should probably accept it. I had wanted to keep the following a comment, but it deserves better formatting, so here goes.

java.time

Since you are using Java 8 (and even if you didn’t), you will prefer the modern more programmer friendly API of java.time:

LocalDate today = LocalDate.of(2017, Month.FEBRUARY, 1);
DateTimeFormatter dateFormat = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
            .withLocale(finLocale);
String formattedDate = today.format(dateFormat);

On my Java 1.8.0_131 it gives the expected

1. helmikuuta 2017

If someone reading this is using Java 6 or 7, please consider getting the ThreeTen Backport library so you can use the modern date and time API as shown.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161