Home  >  Article  >  Java  >  Introduction to the method of judging whether a string is time in java

Introduction to the method of judging whether a string is time in java

尚
Original
2019-12-02 13:55:014031browse

Introduction to the method of judging whether a string is time in java

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn