String to Date with month in Swedish

时间:2019-04-08 13:04:34

标签: java

I have a String value like 2014-maj-06 11:44:39 which I would like to convert into a Java Date, where the month is stored in Swedish format.

I wonder how the pattern for my simpledateformatter should look like?

1 个答案:

答案 0 :(得分:7)

You can inject your DateTimeFormatter with the desired Locale:

DateTimeFormatter.ofPattern("yyyy-LLL-dd hh:mm:ss", new Locale("sv"))
.parse("2014-maj-06 11:44:39");

... will give you (my indents):

{SecondOfMinute=39, HourOfAmPm=11, MinuteOfHour=44, NanoOfSecond=0, 
    MilliOfSecond=0, MicroOfSecond=0},
    ISO resolved to 2014-05-06

API here.

The Locale object will work in conjunction with the LLL format, which matches the abbreviated textual month form, aka in Swedish, maj for May.

Note

For Java < 8, you can use the same format and Locale, and initialize a new SimpleDateFormat instead.