Deo
Deo

Reputation: 45

Fetching only month from the date and giving month name in java

I have fetched the date month and year from the text file so now i want to fetch only month part and I have to get month name I have done like this

String s;
String keyword = "Facture du";

while ((s = br.readLine()) != null) {
    if (s.contains(keyword)) {
    //  s= s.replaceAll("\\D+","");

        System.out.println(s);
    }
}

Actual Output: Facture du 28/05/2018
Expected Output: only Month name

Upvotes: 1

Views: 2396

Answers (3)

Anonymous
Anonymous

Reputation: 86379

Nicholas K already provided an answer nicely showing the use of java.time. I just wanted to add that java.time can do a bit more than shown there.

    DateTimeFormatter factureLineFormatter
            = DateTimeFormatter.ofPattern("'Facture du' dd/MM/uuuu");

    String keyword = "Facture du";
    String s = "Facture du 28/05/2018";
    if (s.contains(keyword)) {
        LocalDate date = LocalDate.parse(s, factureLineFormatter);
        Month month = date.getMonth();  // Extract a `Month` enum object.
        String output = 
            month.getDisplayName(       // Get localized name of month.
                TextStyle.FULL,         // How long or abbreviated should the month name be.
                Locale.FRENCH)          // `Locale` determines the human language used in translation, and the cultural norms used in abbreviation, punctuation, and capitalization.
        ;
        System.out.println(output);
    }

Output:

mai

I am parsing the entire line immediately by adding the literal text in quotes in the format pattern string. I am printing the localized month name — here in French, but you can choose another language. You may also choose to have it abbreviated if you prefer.

Edit: Basil Bourque has kindly edited my code spelling out in comments what each method and argument does. This makes the code look long, but is great for the explanation in a Stack Overflow answer. In production code you would probably use a one-liner:

        System.out.println(date.getMonth().getDisplayName(TextStyle.FULL, Locale.FRENCH));

Upvotes: 1

Nicholas K
Nicholas K

Reputation: 15443

Using java-8's LocalDate you can just do :

String strDate = "28/05/2018";
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.parse(strDate, format);
System.out.println(localDate.getMonth());

which gives the output as MAY

Upvotes: 2

T A
T A

Reputation: 1756

You could use Calendar from the java.utils package:

    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    Date date = format.parse("28/05/2018");
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    System.out.println(cal.getDisplayName(Calendar.MONTH, Calendar.LONG_FORMAT, Locale.FRENCH));

I'm assuming you speak french and want to display the french name. Otherwise you will need to adjust the Locale parameter.

For your date this code will output "mai".

Upvotes: 0

Related Questions