如何在java中使用DateAPI验证年份

如何在java中使用DateAPI验证年份,java,Java,我写了一些验证日期的代码,但为什么不是验证年份呢? 请参阅1234即使没有给出错误,也不是有效年份 try { SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy"); df.setLenient(false); Date date=df.parse("10/4/1234"); System.out.println(

我写了一些验证日期的代码,但为什么不是验证年份呢? 请参阅1234即使没有给出错误,也不是有效年份

try {
        SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");
        df.setLenient(false);
        Date date=df.parse("10/4/1234");
        System.out.println("date------->"+date);
    } catch (java.text.ParseException e) {
       e.printStackTrace();
    }
sql server过程中出现错误:

选择@CONVERT\u DATETIME=CONVERT(DATETIME,@DB\u DATE,103)

将@STORE_DB=CAST((CAST(DATEDIFF(s,'1970-01-01',ltrim(rtrim(@CONVERT_DATETIME)))设置为bigint))*1000-@GMT5)设置为varchar(MAX))


1234年当然是有效的一年

如果需要更多限制,可以使用GregorianCalendar验证年份。 例如:

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(myDate);
if (cal.get(Calendar.YEAR) < 1970) {
    throw new ParseException("Date under 1970 are not allowed!", 6);
}
日历cal=GregorianCalendar.getInstance(); 校准设置时间(myDate); if(日历年)<1970年){ 抛出新的ParseException(“不允许日期低于1970!”,6); }
1234当然是有效的一年

如果需要更多限制,可以使用GregorianCalendar验证年份。 例如:

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(myDate);
if (cal.get(Calendar.YEAR) < 1970) {
    throw new ParseException("Date under 1970 are not allowed!", 6);
}
日历cal=GregorianCalendar.getInstance(); 校准设置时间(myDate); if(日历年)<1970年){ 抛出新的ParseException(“不允许日期低于1970!”,6); }
如前所述,1234年为有效年份。我假设您想要一种方法来确定1234不是需要使用的正确年份

您可以从字符串中提取年份并进行简单的比较

try {
        SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");

        Calendar c = Calendar.getInstance();
        c.setTime(df.parse("10/4/1234"));

        System.out.println("Year = " + c.get(Calendar.YEAR));
        // check the condition whether year is valid
        if( c.get(Calendar.YEAR) < 1970 ) {
            System.out.println("Invalid Year entered");
            // you can throw an exception here if you want
        }
    } catch (java.text.ParseException e) {
       e.printStackTrace();
    }
对于以下格式

SimpleDateFormat("MM/dd/yyyy");

因为12345与前面提到的“yyyy”不匹配,所以1234是一个有效的年份。我假设您想要一种方法来确定1234不是需要使用的正确年份

您可以从字符串中提取年份并进行简单的比较

try {
        SimpleDateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy");

        Calendar c = Calendar.getInstance();
        c.setTime(df.parse("10/4/1234"));

        System.out.println("Year = " + c.get(Calendar.YEAR));
        // check the condition whether year is valid
        if( c.get(Calendar.YEAR) < 1970 ) {
            System.out.println("Invalid Year entered");
            // you can throw an exception here if you want
        }
    } catch (java.text.ParseException e) {
       e.printStackTrace();
    }
对于以下格式

SimpleDateFormat("MM/dd/yyyy");

因为12345不匹配“yyyy”

为什么您认为1234是无效年份?您是说没有1234年吗+1@ChrisMantle不知道有关于年份的维基文章;)通过用户输入,我有一些日期条目,比如9934562012。所以我认为用户可能会犯这样的错误。所以我想确认日期。我想他在发帖时漏掉了一些负号。为什么你认为1234年不是有效的年份?你是说没有1234年吗+1@ChrisMantle不知道有关于年份的维基文章;)通过用户输入,我有一些日期条目,比如9934562012。所以我认为用户可能会犯这样的错误。所以我想确认一下日期。我想他在发帖时漏掉了一些负号