Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting nominative month name in jodaTime

Tags:

java

jodatime

I need to get month name according to it's number in different Locales.

To do this I create a DateTime (or YearMonth, doesn't matter) Object and get its monthOfYear Property:

YearMonth md = new YearMonth(1992, month);
System.out.println(md.monthOfYear().getAsText(new Locale("ru")));

The thing is that works fine for English language, because Nominative and Genitive names are the same, but for other languages it returns only Genitive, which is not what I need.

How to get Nominative month name?

like image 749
troy Avatar asked May 19 '14 13:05

troy


People also ask

How to convert a month number to a month name?

Let us discuss different ways to convert a month number to a month name. We will get the month number by taking input from the user, from date, or by creating a series of dates and then converting the month number to month name. We will see that the month name can be printed in two ways.

How to get the full name of the month as of March?

The first way is the full name of the month as of March and another way is the short name like Mar. This method uses datetime module. The user gives the input for the month number. datetime.strptime () is called. It takes month number and month format "%m" as arguments.

How to find the equivalent month name in Python calendar?

There are two built-in arrays in the calendar module. calendar.month_name [] is an array that represents the full month name while calendar.month_abbr [] in the second array that represents the abbreviated month names. You can also assign any month number to x in order to find the equivalent month name. This method uses Pandas library of Python.

How to iterate over the number of months in a calendar?

Passing "%b" to strftime returns abbreviated month name while using "%B" returns full month name. This method imports calender module. It simply uses a for loop to iterate over the number of months (12).


1 Answers

I think you're confusing genitive with nominative. When I run the following code:

import org.joda.time.YearMonth;
import java.util.Locale;

public class X {

    public static void main(String[] s){
        for (int i = 1;i<13; i++) {
            YearMonth md = new YearMonth(1992, i);
            System.out.println(md.monthOfYear().getAsText(new Locale("ru")));
        }
    }
}

I get the following result:

Январь
Февраль
Март
Апрель
Май
Июнь 
Июль
Август
Сентябрь
Октябрь
Ноябрь
Декабрь

Which is, for my Russian knowledge :) nominative. You probably need to get genitive, but I doubt Joda supports it. You're free to implement it yourself, please see this question: Month name in genitive (Polish locale) with Joda-Time DateTimeFormatter

like image 151
Marcin Szawurski Avatar answered Sep 24 '22 00:09

Marcin Szawurski