Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

java - How to check if a string is date?

I am having string like

"11-04-2015 22:01:13:053" or "32476347656435"

How can I check if string is Date?
Checking String if it is numeric using regex

String regex = "[0-9]+";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Other person are also correct

This is your answer

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class date {
    public static boolean isValidDate(String inDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:ms");
        dateFormat.setLenient(false);
        try {
            dateFormat.parse(inDate.trim());
        } catch (ParseException pe) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

        System.out.println(isValidDate("20-01-2014"));
        System.out.println(isValidDate("11-04-2015 22:01:33:023"));

        System.out.println(isValidDate("32476347656435"));
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...