2

假设英文句子:

String s = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";

还有中文句子:

String s = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

我试过

  String[] words = s.split("\\s+");
  for (int i = 0; i < words.length; i++) {
  System.out.println(words[i]);
  }

我得到了英文句子

13:10:47,829 INFO  [STDOUT] Your
13:10:47,829 INFO  [STDOUT] Last
13:10:47,829 INFO  [STDOUT] Login
13:10:47,829 INFO  [STDOUT] was
13:10:47,829 INFO  [STDOUT] 2013/10/04
13:10:47,829 INFO  [STDOUT] 13:06:45
13:10:47,829 INFO  [STDOUT] (
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Days,
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Hours,
13:10:47,829 INFO  [STDOUT] 0
13:10:47,829 INFO  [STDOUT] Minutes
13:10:47,829 INFO  [STDOUT] )

而对于中文句子:

13:11:49,712 INFO  [STDOUT] 您上次登录是
13:11:49,712 INFO  [STDOUT] 2013/10/04
13:11:49,712 INFO  [STDOUT] 13:07:15(
13:11:49,712 INFO  [STDOUT] 0
13:11:49,712 INFO  [STDOUT] 天,
13:11:49,712 INFO  [STDOUT] 0
13:11:49,712 INFO  [STDOUT] 小时
13:11:49,712 INFO  [STDOUT] 4
13:11:49,712 INFO  [STDOUT] 分钟
13:11:49,712 INFO  [STDOUT] )

结果,我可以很容易地从英文句子中获取日期、时间和整数值。但是当句子更改为中文时,它无法使用相同的方式获取值。因为句子拆分后的数组数量不同。有没有什么方法可以从一个句子中获取日期、时间和整数值,即使是不同的语言,句子拆分后返回的数组数量也不同。

4

4 回答 4

4
 public static void main(String[] args) {
    String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
    String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";

    String datePattern = "\\d\\d\\d\\d/\\d\\d/\\d\\d"; 
    String timePattern = "\\d\\d:\\d\\d:\\d\\d";

    System.out.println(getMatch(english, datePattern));
    System.out.println(getMatch(english, timePattern));
    System.out.println(getMatch(english, "\\d Days"));
    System.out.println(getMatch(english, "\\d Hours"));
    System.out.println(getMatch(english, "\\d Minutes"));
    System.out.println();
    System.out.println(getMatch(chinese, datePattern));
    System.out.println(getMatch(chinese, timePattern));
    System.out.println(getMatch(chinese, "\\d 天"));
    System.out.println(getMatch(chinese, "\\d 小时"));
    System.out.println(getMatch(chinese, "\\d 分钟"));
}

private static String getMatch(String input, String regex) {
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    if (matcher.find()) {
        return matcher.group();
    } else {
        return "";
    }
}
于 2013-10-04T05:56:03.113 回答
1

java.time

尽管我将为您提供的两种解决方案都适用于旧的日期时间 API(java.util日期时间类型及其格式化 API SimpleDateFormat),但请记住,此 API 已过时且容易出错,因此,它是建议完全停止使用它并切换到java.time现代日期时间 API *。下面给出的两种解决方案都使用java.time API。

使用 RegEx 和java.time API 的解决方案:

您可以使用regex\d{1,4}\/\d{1,2}\/\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}从文本中检索日期时间字符串。我建议您浏览这些链接以了解 RegEx 和Java RegEx API。您可以借助以下几点来理解这个特定的 RegEx:

  • \d{1,4}: 1 到 4 位数
  • \/:字符文字,/

演示:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
        String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )&quot;;

        // Assuming the date-time string is in the format, yyyy/MM/dd HH:mm:ss
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/M/d H:m:s");

        // Processing english
        LocalDateTime dt = LocalDateTime.parse(getDateTime(english), dtf);
        System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n", dt.getYear(),
                dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond());

        // Processing chinese
        dt = LocalDateTime.parse(getDateTime(chinese), dtf);
        System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n", dt.getYear(),
                dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond());
    }

    static String getDateTime(String s) {
        Matcher matcher = Pattern.compile("\\d{1,4}\\/\\d{1,2}\\/\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}").matcher(s);
        String strDateTime = "";
        if (matcher.find()) {
            strDateTime = matcher.group();
        }
        return strDateTime;
    }
}

输出:

Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45
Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45

ONLINE DEMO

解决方案,纯粹使用java.time API:

import java.text.ParsePosition;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Optional;

public class Main {
    public static void main(String[] args) {
        String english = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
        String chinese = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )&quot;;

        // Processing english
        Optional<LocalDateTime> date = getDateTime(english);
        date.ifPresent(dt -> System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n",
                dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond()));

        // Processing chinese
        date = getDateTime(chinese);
        date.ifPresent(dt -> System.out.printf("Year: %d, Month: %d, Day: %d, Hour: %d, Minute: %d, Second: %d%n",
                dt.getYear(), dt.getMonthValue(), dt.getDayOfMonth(), dt.getHour(), dt.getMinute(), dt.getSecond()));
    }

    static Optional<LocalDateTime> getDateTime(String s) {
        // Assuming the date-time string is in the format, yyyy/MM/dd HH:mm:ss
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/M/d H:m:s");
        
        Optional<LocalDateTime> result = Optional.empty();
        for (int i = 0; i < s.length(); i++) {
            try {
                result = Optional.ofNullable(LocalDateTime.from(dtf.parse(s, new ParsePosition(i))));
                break;
            } catch (DateTimeParseException | IndexOutOfBoundsException e) {
            }
        }
        return result;
    }
}

输出:

Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45
Year: 2013, Month: 10, Day: 4, Hour: 13, Minute: 6, Second: 45

ONLINE DEMO

Trail: Date Time了解有关现代日期时间 API *的更多信息。


* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和 Android API 工作level 仍然不符合 Java-8,请检查Java 8+ APIs available through desugaringHow to use ThreeTenABP in Android Project

于 2021-05-06T18:09:07.377 回答
0

您可以在此处找到日期和时间。您也可以按照相同的方式获取 Integer 值(尽管我仍然不知道您在谈论哪些 Integer 值)。

String s = "Your Last Login was 2013/10/04 13:06:45 ( 0 Days, 0 Hours, 0 Minutes )";
String[] words = s.split("\\s+");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String datePlusTime = null;
for (int i = 0; i < words.length; i++) {
    try {
        sdf.parse(words[i]);
        // It comes here, that means its the date we want
        datePlusTime = words[i] +" "+ words[i + 1]; // This has date plus the time
        // I concatenated them just for an example. You can do whatever you want with them.
        break;
    } catch (ParseException pe) {
        // Eat the exception
        // I must say this is not a good practice though
    }
}

System.out.println(datePlusTime); // Do whatever you want with it.

PS:-这是假设 和 之间有45空格(。如果没有,那么您需要相应地删除words[i+1]字符串的最后一个字符。

于 2013-10-04T05:43:35.793 回答
0

如果您的字符串是静态的,那么您可以尝试如下所示。

 String s = "您上次登录是 2013/10/04 13:06:45( 0 天, 0 小时 0 分钟 )";
 System.out.println( s.substring(6, 17)  + "  -- " +   s.substring(18,26) + "-- " +s.substring(28, 29) + "-- " +s.substring(33, 34) + "-- " +s.substring(38, 39));

可能这不是一件好事。

于 2013-10-04T05:49:57.920 回答