简体   繁体   中英

How to get a month as an integer from the given date and print as month name format (“MMM”)

I am new to Java and couldnt retrieve the month while using the below code instead month value is set to 0. Please advise the mistakes that i have done here.

*

for(int i=0;i<this.input.size();i++)
     {
         SimpleDateFormat sf = new SimpleDateFormat("dd/mm/yyyy");

         Date purchasedate;
        try {
            String details  = input.get(i);
            String[] detailsarr = details.split(",");
            purchasedate = sf.parse(detailsarr[1]);
            Calendar cal = Calendar.getInstance();
            cal.setTime(purchasedate);
            int month = cal.get(Calendar.MONTH);

* After getting the above month as an integer, Could you please advise if there is anyway to print the above month value as "MMM" format?

tl;dr

LocalDate.parse(          // Represent a date-only value, without time-of-day and without time zone.
    "23/01/2018" ,        // Tip: Use standard ISO 8601 formats rather than this localized format for data-exchange of date-time values.
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) 
)                         // Return a `LocalDate` object.
.getMonth()               // Return a `Month` enum object representing the month of this date.
.getDisplayName(          // Automatically localize, generating text of the name of this month.
    TextStyle.SHORT ,     // Specify (a) how long or abbreviated, and (b) specify whether used in stand-alone or combo context linguistically (irrelevant in English). 
    Locale.US             // Specify the human language and cultural norms to use in translation.
)                         // Returns a `String`.

See this code run live at IdeOne.com .

Jan

java.time

The modern approach uses the java.time classes that supplanted the terrible Date / Calendar / SimpleDateFormat classes.

ISO 8601

Tip: When exchanging date-time values as text, use the ISO 8601 standard formats rather than using text meant for presentation to humans. For a date-only value, that would be YYYY-MM-DD such as 2018-01-23.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;
LocalDate ld = LocalDate.parse( "23/01/2018" , f ) ;

Month enum

Retrieve the month as a Month enum object.

Month m = ld.getMonth() ;

Localize

Ask that Month enum to generate a String with text of the name of the month. The getDisplayName method can automatically localize for you. To localize, specify:

  • TextStyle to determine how long or abbreviated should the string be. Note that in some languages you may need to choose stand-alone style depending on context in which you intend to use the result.
  • Locale to determine:
    • The human language for translation of name of day, name of month, and such.
    • The cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Code:

String output = m.getDisplayName( TextStyle.SHORT , Locale.US ) ;

Use enum, not integer

Notice that we had no use of an integer number to represent the month. Using an enum object instead makes our code more self-documenting, ensures valid values, and provides type-safety .

So I strongly recommend passing around Month objects rather than mere int integer numbers. But if you insist, call Month.getMonthValue() to get a number. The numbering is sane, 1-12 for January-December, unlike the legacy classes.

int monthNumber = ld.getMonthValue() ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.

To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .

java.time

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");

    String dateStringFromInput = "29/08/2018";
    LocalDate purchasedate = LocalDate.parse(dateStringFromInput, dateFormatter);

    int monthNumber = purchasedate.getMonthValue();
    System.out.println("Month number is " + monthNumber);

Running the above snippet gives this output:

Month number is 8

Note that contrary to Calendar LocalDate numbers the months the same way humans do, August is month 8. However to get the month formatted into a standard three letter abbreviation we don't need the number first:

    Locale irish = Locale.forLanguageTag("ga");
    DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMM", irish);

    String formattedMonth = purchasedate.format(monthFormatter);
    System.out.println("Formatted month: " + formattedMonth);

Formatted month: Lún

Please supply your desired locale where I put Irish/Gaelic. Java knows the month abbreviations in a vast number of languages.

What went wrong in your code?

Apart from using the long outdated date and time classes, SimpleDateFormat , Date and Calendar , format pattern letters are case sensitive (this is true with the modern DateTimeFormatter too). To parse or format a month you need to use uppercase M (which you did correctly in your title). Lowercase m is for minute of the hour. SimpleDateFormat is troublesome here (as all too often): rather than telling you something is wrong through an exception it just tacitly defaults the month to January. Which Calendar in turn returns to you as month 0 because it unnaturally numbers the months from 0 through 11.

Links

Simple way of doing this is

Calendar cal = Calendar.getInstance();
Date d = cal.getTime();     
SimpleDateFormat sdf = new SimpleDateFormat("MMM");
System.out.println(sdf.format(d));

In your case modify snippet like below:

 SimpleDateFormat sf = new SimpleDateFormat("dd/mm/yyyy");
 Date purchasedate;
 try {
        String details  = input.get(i);
        String[] detailsarr = details.split(",");
        purchasedate = sf.parse(detailsarr[1]);
        SimpleDateFormat sdf = new SimpleDateFormat("MMM");
        String month = sdf.format(purchasedate);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM