Java method to determine whether a string is time: (Recommended: java video tutorial)
Introduce the jar package:
import org.apache.commons.lang3.time.DateUtils;
java determines whether it is a time code:
private static String[] parsePatterns = {"yyyy-MM-dd","yyyy年MM月dd日", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd"}; public static Date parseDate(String string) { if (string == null) { return null; } try { return DateUtils.parseDate(str, parsePatterns); } catch (ParseException e) { return null; } }
You can determine by calling the parseDate method. If you need to determine other formats, just add them to parsePatterns.
For more java knowledge, please pay attention to the java basic tutorial column.
The above is the detailed content of Introduction to the method of judging whether a string is time in java. For more information, please follow other related articles on the PHP Chinese website!