从日期中仅获取月份,并在Java中指定月份名称

时间:2019-02-13 13:18:56

标签: java string date

我已经从文本文件中获取了日期月份和年份,所以现在我只想获取月份的一部分,我必须获取我已经这样做的月份名称

String s;
String keyword = "Facture du";

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

        System.out.println(s);
    }
}

实际输出:Facture du 28/05/2018
预期的输出:仅月份名称

4 个答案:

答案 0 :(得分:2)

使用java-8的LocalDate可以做到:

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

将输出显示为MAY

答案 1 :(得分:1)

Nicholas K已经提供了一个很好的答案,很好地展示了java.time的使用。我只是想补充一点,java.time可以做的比这里显示的还要多。

    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);
    }

输出:

  

mai

我通过在格式模式字符串的引号中添加文字文本来立即解析整行。我正在打印本地化的月份名称-在这里是法语,但是您可以选择其他语言。如果愿意,也可以选择缩写。

编辑:Basil Bourque很好地编辑了我的代码,在注释中详细说明了每种方法和参数的作用。这使代码看起来很长,但是对于堆栈溢出答案中的解释非常有用。在生产代码中,您可能会使用单线:

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

答案 2 :(得分:0)

//required imports
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

//your class declaration, other methods, etc...

//you should declare your Pattern here, for performance reasons:

private static final Pattern pat = Pattern.compile("\\d\\d/\\d\\d/\\d\\d\\d\\d");

//the actual month extractor:
public String getMonthFromString(String s) {
    //Your Pattern object uses the regular expression for your date,
    //to extract it from your String.
    //Then, you use a Matcher to search through your String:

    Matcher mat = pat.matcher(s);

    //The matcher can find every occurence of the pattern on your String.
    //Let's assume you only want the first:

    if(mat.find()) {
        String date = mat.group();

        //Now you have your date `String`. From there, you can parse
        //your date to a LocalDate object and extract its month:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate dateObj = LocalDate.parse(date, formatter);

        String month = dateObj.getMonth();
        return month;
    }
    else {
        //handle situations when the String doesn't hold a date.
    }
}

答案 3 :(得分:0)

您可以使用Calendar包中的java.utils

    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));

我假设您说法语,并想显示法语名称。否则,您将需要调整Locale参数。

对于您的日期,此代码将输出“ mai”。