如何检查字符串是否为日期? [英] How to check if a string is date?

查看:72
本文介绍了如何检查字符串是否为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像这样的字符串

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

如何检查字符串是否为日期?
使用正则表达式检查String是否为数字

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

String regex = "[0-9]+";

推荐答案

其他人也是正确的

Other person are also correct

这是你的答案

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"));
    }
}

这篇关于如何检查字符串是否为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆