[英]Get Date, Time and Integer value from a sentence java
假设该句子为英语:
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] )
结果,我可以轻松地从英文句子中获取日期,时间和整数值。 但是当句子更改为中文时,无法使用相同的方式获得价值。 因为句子拆分后的数组个数不同。 我有什么办法可以从句子中获取日期,时间和整数值,即使使用不同的语言,也可以从句子拆分后返回的数组数不同。
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 "";
}
}
您可以在此处找到日期和时间。 您也可以按照相同的方式获取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]
字符串的最后一个字符。
如果您的字符串是静态的,则可以尝试以下操作。
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));
可能这不是一个好东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.