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 分钟 )";
// 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 分钟 )";
// 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 desugaring和How to use ThreeTenABP in Android Project。