Java:检查当前字符串的日期格式是否符合要求的格式 [英] Java: Check the date format of current string is according to required format or not

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

问题描述

我想知道在 Java 中可以使用哪种方法可以执行此操作。否则我可能会使用Regex解决方案。


我有输入来自用户的字符串可以是任何字符。而且我想检查输入字符串是否符合我所需的日期格式。

I wanted to know that is there any method available in Java that can do this.Otherwise I may go for Regex solution.

I have input string from user that can be any characters. And I want to check that the input string is according to my required date format or not.

由于我输入了 20130925 ,我所需的格式是 dd / MM / yyyy 所以,在这种情况下,我应该得到 false



我不想转换这个日期我只是想检查输入字符串是否是根据所需的日期格式。

As I have input 20130925 and my required format is dd/MM/yyyy so, for this case I should get false.

I don't want to convert this date I just want to check whether input string is according to required date format or not.




我尝试过以下



I have tried following

Date date = null;
try {
date = new SimpleDateFormat("dd/MM/yyyy").parse("20130925");
} catch (Exception ex) {
// do something for invalid dateformat
}

但是我的catch(Exception ex)块无法捕获由SimpleDateFormat.Parse()生成的任何异常;

but my catch (Exception ex) block is unable to catch any exceptions generated by SimpleDateFormat.Parse();

推荐答案

简单的尝试并解析 String 到所需的日期使用类似 SimpleDateFormat

Simple try and parse the String to the required Date using something like SimpleDateFormat

Date date = null;
try {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    date = sdf.parse(value);
    if (!value.equals(sdf.format(date))) {
        date = null;
    }
} catch (ParseException ex) {
    ex.printStackTrace();
}
if (date == null) {
    // Invalid date format
} else {
    // Valid date format
}

然后,您可以简单地编写一个执行此操作的简单方法,并返回 true 当有日期不为空...

You could then simply write a simple method that performed this action and returned true when ever Date was not null...

作为建议...

用运行示例更新

我不知道你在做什么,以下示例...

I'm not sure what you are doing, but, the following example...

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestDateParser {

    public static void main(String[] args) {
        System.out.println("isValid - dd/MM/yyyy with 20130925 = " + isValidFormat("dd/MM/yyyy", "20130925"));
        System.out.println("isValid - dd/MM/yyyy with 25/09/2013 = " + isValidFormat("dd/MM/yyyy", "25/09/2013"));
        System.out.println("isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = " + isValidFormat("dd/MM/yyyy", "25/09/2013  12:13:50"));
    }

    public static boolean isValidFormat(String format, String value) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            date = sdf.parse(value);
            if (!value.equals(sdf.format(date))) {
                date = null;
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return date != null;
    }

}

输出(类似)

java.text.ParseException: Unparseable date: "20130925"
isValid - dd/MM/yyyy with 20130925 = false
isValid - dd/MM/yyyy with 25/09/2013 = true
isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = false
    at java.text.DateFormat.parse(DateFormat.java:366)
    at javaapplication373.JavaApplication373.isValidFormat(JavaApplication373.java:28)
    at javaapplication373.JavaApplication373.main(JavaApplication373.java:19)

这篇关于Java:检查当前字符串的日期格式是否符合要求的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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