java.time
Even though the two solutions I am going to provide you with will work with the legacy date-time API (java.util
date-time types and their formatting API, SimpleDateFormat
), keep in mind that this API is outdated and error-prone and therefore, It is recommended to stop using it completely and switch to java.time
, the modern date-time API*. Both of the solutions given below use java.time API.
Solution using RegEx and java.time API:
You can use the regex, \d{1,4}\/\d{1,2}\/\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}
to retrieve the date-time string from the text. I recommend you explore the links to understand the RegEx and Java RegEx API. You can understand this specific RegEx with help of the following points:
\d{1,4}
: 1 to 4 digits
\/
: The character literal, /
Demo:
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;
}
}
Output:
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
Solution, purely using 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;
}
}
Output:
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
Learn more about the the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.