Month name as a string

前端 未结 11 2027
悲哀的现实
悲哀的现实 2020-11-27 02:49

I\'m trying to return the name of the month as a String, for instance \"May\", \"September\", \"November\".

I tried:

int month = c.get(Calendar.MONTH         


        
相关标签:
11条回答
  • 2020-11-27 03:40

    Use this :

    Calendar cal=Calendar.getInstance();
    SimpleDateFormat month_date = new SimpleDateFormat("MMMM");
    String month_name = month_date.format(cal.getTime());
    

    Month name will contain the full month name,,if you want short month name use this

     SimpleDateFormat month_date = new SimpleDateFormat("MMM");
     String month_name = month_date.format(cal.getTime());
    
    0 讨论(0)
  • 2020-11-27 03:40

    The only one way on Android to get properly formatted stanalone month name for such languages as ukrainian, russian, czech

    private String getMonthName(Calendar calendar, boolean short) {
        int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_MONTH_DAY | DateUtils.FORMAT_NO_YEAR;
        if (short) {
            flags |= DateUtils.FORMAT_ABBREV_MONTH;
        }
        return DateUtils.formatDateTime(getContext(), calendar.getTimeInMillis(), flags);
    }
    

    Tested on API 15-25

    Output for May is Май but not Мая

    0 讨论(0)
  • 2020-11-27 03:42

    I keep this answer which is useful for other cases, but @trutheality answer seems to be the most simple and direct way.

    You can use DateFormatSymbols

    DateFormatSymbols(Locale.FRENCH).getMonths()[month]; // FRENCH as an example
    
    0 讨论(0)
  • 2020-11-27 03:45

    "MMMM" is definitely NOT the right solution (even if it works for many languages), use "LLLL" pattern with SimpleDateFormat

    The support for 'L' as ICU-compatible extension for stand-alone month names was added to Android platform on Jun. 2010.

    Even if in English there is no difference between the encoding by 'MMMM' and 'LLLL', your should think about other languages, too.

    E.g. this is what you get, if you use Calendar.getDisplayName or the "MMMM" pattern for January with the Russian Locale:

    января (which is correct for a complete date string: "10 января, 2014")

    but in case of a stand-alone month name you would expect:

    январь

    The right solution is:

     SimpleDateFormat dateFormat = new SimpleDateFormat( "LLLL", Locale.getDefault() );
     dateFormat.format( date );
    

    If you are interested in where all the translations come from - here is the reference to gregorian calendar translations (other calendars linked on top of the page).

    0 讨论(0)
  • 2020-11-27 03:46

    Russian.

    Month
    .MAY
    .getDisplayName(
        TextStyle.FULL_STANDALONE ,
        new Locale( "ru" , "RU" )
    )
    

    май

    English in the United States.

    Month
    .MAY
    .getDisplayName(
        TextStyle.FULL_STANDALONE ,
        Locale.US
    )
    

    May

    See this code run live at IdeOne.com.

    ThreeTenABP and java.time

    Here’s the modern answer. When this question was asked in 2011, Calendar and GregorianCalendar were commonly used for dates and times even though they were always poorly designed. That’s 8 years ago now, and those classes are long outdated. Assuming you are not yet on API level 26, my suggestion is to use the ThreeTenABP library, which contains an Android adapted backport of java.time, the modern Java date and time API. java.time is so much nicer to work with.

    Depending on your exact needs and situation there are two options:

    1. Use Month and its getDisplayName method.
    2. Use a DateTimeFormatter.

    Use Month

        Locale desiredLanguage = Locale.ENGLISH;
        Month m = Month.MAY;
        String monthName = m.getDisplayName(TextStyle.FULL, desiredLanguage);
        System.out.println(monthName);
    

    Output from this snippet is:

    May

    In a few languages it will make a difference whether you use TextStyle.FULL or TextStyle.FULL_STANDALONE. You will have to see, maybe check with your users, which of the two fits into your context.

    Use a DateTimeFormatter

    If you’ve got a date with or without time of day, I find a DateTimeFormatter more practical. For example:

        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", desiredLanguage);
    
        ZonedDateTime dateTime = ZonedDateTime.of(2019, 5, 31, 23, 49, 51, 0, ZoneId.of("America/Araguaina"));
        String monthName = dateTime.format(monthFormatter);
    

    I am showing the use of a ZonedDateTime, the closest replacement for the old Calendar class. The above code will work for a LocalDate, a LocalDateTime, MonthDay, OffsetDateTime and a YearMonth too.

    What if you got a Calendar from a legacy API not yet upgraded to java.time? Convert to a ZonedDateTime and proceed as above:

        Calendar c = getCalendarFromLegacyApi();
        ZonedDateTime dateTime = DateTimeUtils.toZonedDateTime(c);
    

    The rest is the same as before.

    Question: Doesn’t java.time require Android API level 26?

    java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

    • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
    • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
    • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

    Links

    • Oracle tutorial: Date Time explaining how to use java.time.
    • Java Specification Request (JSR) 310, where java.time was first described.
    • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
    • ThreeTenABP, Android edition of ThreeTen Backport
    • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
    0 讨论(0)
提交回复
热议问题