Home > Java > JavaBase > 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

Release: 2019-12-02 13:55:01
Original
4071 people have browsed it

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;
Copy after login

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;
        }
    }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template