从字符串 Java 中提取葡萄牙语日期

Extract date in Portuguese from String Java

我想从一个字符串中提取数据,而这个字符串有时会以不同的方式出现。例如,它可以是以下任何一项:

Portaria n° 200, 28 de janeiro de 2018.

Portaria n° 200, 28 de janeiro de 2018 da Republica Brasileira.

Portaria n° 200 28 de janeiro de 2018.

Portaria n° 200 2017/2018 de 28 de janeiro de 2018.

没有模式。我已经尝试过 xsplit: 它在某些情况下有效,但并非一直有效。

    String receberTextoIdentifica = (xmlUtil.xpathElement(documentOrigem, Constantes.GETIDENTIFICACAO).getTextContent());
    LocalDateTime receberDataEnvio = materiaDto.getDataEnvio();
    Integer receberDataEnvioAno = receberDataEnvio.getYear();
    if (receberTextoIdentifica != null && receberTextoIdentifica.toLowerCase().contains("" + receberDataEnvioAno)) {
        Element dataTexto = documentDestino.createElement("dataTexto");
        estruturas.appendChild(dataTexto);
        receberTextoIdentifica = receberTextoIdentifica.substring(0, receberTextoIdentifica.indexOf("" + receberDataEnvioAno) + 4);
        String words[] = receberTextoIdentifica.split(" ");
        String lastFive = words[words.length - 5] + " " + words[words.length - 4] + " " + words[words.length - 3] + " "
                + words[words.length - 2] + " " + words[words.length - 1];
        dataTexto.setTextContent(lastFive);

首先使用正则表达式在字符串中查找日期,然后使用 DateTimeFormatter 将其解析为 LocalDate:

    Pattern datePattern = Pattern.compile("\d{1,2} de [a-zç]{4,9} de \d{4}");
    DateTimeFormatter portugueseDateFormatter
            = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)
                    .withLocale(Locale.forLanguageTag("pt-BR"));

    String[] differentStrings = {
            "Portaria n° 200, 28 de janeiro de 2018.",
            "Portaria n° 200, 28 de janeiro de 2018 da Republica Brasileira.",
            "Portaria n° 200 28 de janeiro de 2018.",
            "Portaria n° 200 2017/2018 de 28 de janeiro de 2018."
    };

    for (String s : differentStrings) {
        Matcher m = datePattern.matcher(s);
        if (m.find()) {
            String dateString = m.group();
            LocalDate date = LocalDate.parse(dateString, portugueseDateFormatter);
            System.out.println("Date found: " + date);
        } else {
            System.out.println("No date found in " + s);
        }
    }

输出为:

Date found: 2018-01-28
Date found: 2018-01-28
Date found: 2018-01-28
Date found: 2018-01-28

正则表达式接受一位或两位数字表示月份,然后是 de(前后有 space),月份名称的四到九个小写字母,包括 çmarço(三月)一样,de又是一个四位数的年份。

您可能希望从解析中捕获 DateTimeParseException,甚至可能再次尝试 find 以查看实际日期是否出现在字符串的后面。

@Ole 建议的 的替代方法。

该方法按原样从字符串中获取数据,而不将其转换为日期对象。

代码:

import java.util.Scanner;
import java.util.Arrays;
import java.util.List;

class Main {

  public static void main(String[] args) {

  String[] strs = {
            "Portaria n° 200, 28 de janeiro de 2018",
            "Portaria n° 200, 28 de janeiro de 2018 da Republica Brasileira",
            "Portaria n° 200 28 de janeiro de 2018",
            "Portaria n° 200 2017/2018 de 25 de janeiro de 2018"
    };

    String months[] = {"janeiro", "fevereiro", "marco", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"};

    int i,j; 

    for(i = 0; i < strs.length; i++) {
      String test_array [] = strs[i].split(" ");

      for (j = 3; j < test_array.length - 2; j++) {
        if(Arrays.asList(months).contains(test_array[j])) {
          System.out.println(test_array[j-2]+ " " + test_array[j-1]+" " +test_array[j]+ " " +test_array[j+1]+ " " +test_array[j+2]);
        }
      }
    }
  }
}

输出:

28 de janeiro de 2018
28 de janeiro de 2018
28 de janeiro de 2018
25 de janeiro de 2018

查看实际效果 here