1

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

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Deo
  • 45
  • 8
  • You can use `split("/")` to break it into an array, get the month and then use a `switch` to determine the name – Ayrton Feb 13 '19 at 13:21
  • Currently you just print the read line out. You need to parse the Date from the String and get the month name from that and just print this out. – CodeMatrix Feb 13 '19 at 13:21
  • A solution would be to parse the date text into a [LocalDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html) object and then format the date to pattern `MM`. On [How to parse/format dates with LocalDateTime? (Java 8)](https://stackoverflow.com/questions/22463062/how-to-parse-format-dates-with-localdatetime-java-8) thread there are some details about how this can be done –  Feb 13 '19 at 13:45

3 Answers3

2

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

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
1

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));
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

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".

T A
  • 1,677
  • 4
  • 21
  • 29
  • 4
    These terrible date-time classes were supplanted years ago by the *java.time* classes. Suggesting their use in 2019 is poor advice. – Basil Bourque Feb 13 '19 at 17:36
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Feb 13 '19 at 18:15