检查日期是否存在 [英] Checking if a date exists or not

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

问题描述

在java中有任何预定义的类,如果我传递一个日期,它应该返回,如果它是一个有效的日期或不.........说,例如,如果我通过它的2月31日那么一年...那么它应该让我错误...如果日期存在,那么它应该让我真的...任何一年的任何日期...我也想要一个方法,会告诉我这个工作日这个特定的日期是...?
我经历了Calender课程,但我没有得到我们如何做...请帮助....

解决方案

如何使用Java验证日期

  private static boolean isValidDate(String input){
String formatString =MM / dd / yyyy

try {
SimpleDateFormat format = new SimpleDateFormat(formatString);
format.setLenient(false);
format.parse(input);
} catch(ParseException e){
return false;
} catch(IllegalArgumentException e){
return false;
}

返回true;
}

public static void main(String [] args){
System.out.println(isValidDate(45/23/234)); // false
System.out.println(isValidDate(12/12/2111)); // true
}


Is there any predefined class in java such that, if i pass it a date it should return if it is a valid date or not......... say for example if i pass it 31st of February of some year... then it should return me false... and if the date exists then it should return me true... for any date of any year... and i also want a method that would tell me what weekday this particular date is...?? I went through the Calender class but i didn't get how we do this... please help....

解决方案

How to Validate a Date in Java

private static boolean isValidDate(String input) {
        String formatString = "MM/dd/yyyy";

        try {
            SimpleDateFormat format = new SimpleDateFormat(formatString);
            format.setLenient(false);
            format.parse(input);
        } catch (ParseException e) {
            return false;
        } catch (IllegalArgumentException e) {
            return false;
        }

        return true;
    }

public static void main(String[] args){
        System.out.println(isValidDate("45/23/234")); // false
        System.out.println(isValidDate("12/12/2111")); // true
    }

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

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