Proper Russian month string translation Java

后端 未结 5 1812
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 10:53

I want to convert a Date in to Russian and using the code below

SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG,locale).format(date);
<
5条回答
  •  旧时难觅i
    2020-12-09 11:41

    For Java 8 you can use a new pattern.

    In short: The "LLLL" pattern will get a Nominative case:

    new SimpleDateFormat("LLLL", Locale.getDefault()).format(date); // январь
    

    The "MMMM" pattern will return a String in Genitive case:

    new SimpleDateFormat("MMMM", Locale.getDefault()).format(date); // января
    

    Alternatively, instead of hardcoding Russian months in array (since we have Polish, Ukrainian and other languages), you could use the java.time.Month enum. It contains both months int number and String name.

提交回复
热议问题