SimpleDateFormat的错误 [英] SimpleDateFormat bug

查看:207
本文介绍了SimpleDateFormat的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 /**
 * Validates a date in String data type according to the given date format.
 * 
 * @param validDateFormat
 *           valid date format e.g. YYYY/MM/DD
 * @param strDate
 *            - the date to be validated
 * @return true if the date is valid according to the given format
 */
public static boolean isValidDate(final String strDate, final String validDateFormat) {
    boolean result = true;

    try {
        if (StringUtils.isNotBlank(validDateFormat) && StringUtils.isNotBlank(strDate)
                && StringUtils.isNotEmpty(strDate)) {
            dateValidator = new SimpleDateFormat(validDateFormat, Locale.ENGLISH);
            dateValidator.setLenient(false);
            dateValidator.parse(strDate);


            Log.i(TAG, "***Date entered: "+ strDate +
                    "\nDate parsed back: "+dateValidator.format(dateValidator.parse(strDate)));

            dateValidator = null;
        }
        else
        {
            result = false;
        }
    } catch (final ParseException e) {
        result = false;
    } catch (final IllegalArgumentException e) {
        result = false;
    } catch (final Exception e) {
        result = false;
    }

    return result;
}

当我在输入例:

boolean result = isValidDate("21/May/201l", "dd/MMM/yyyy");
//result = true

提醒的是,输入不是两千十一点,其两百一+一个字母l。

be reminded that the input is not two-thousand-eleven, its two-hundred and one + a letter 'l'.

日志:

  ***Date entered: 21/May/201l
   Date parsed back: 21/May/0201

它不抛出任何异常,并且不让它假!!!!

it doesn't throw any exception, and doesn't make it false!!!!

WHYYYYYYYYYY!?

WHYYYYYYYYYY!?

推荐答案

更新:一个想法是强加一个相当严格的限制你的输入:输入的日期的字符串的格式的字符串必须匹配的究竟的;你可以通过做一些像执行本:

Update: One idea would be to impose a rather severe restriction on your inputs: the input date string and format string must match exactly; you could enforce this by doing something like:

Date parsed = dateValidator.parse(strDate);
return strDate.equals(dateValidator.format(parsed));

在换句话说,确认格式化实际日期对象返回一个字符串,它等同于任何在过去。这可以确保你不会有错误的的时候有格式字符串实际上无效字符的结果(因为这些东西会为宗旨被忽略重新格式化日期)。

In other words, verify that formatting the actual Date object returns a string that is identical to whatever was passed in. This ensures that you won't have erroneous positive results when there are actually invalid characters in the format string (since these will be ignored for the purposes of re-formatting the date).

我不知道它在总体良好的解决方案,因为它可能是过于严格。我真的没有想到它通过非常谨慎。但无论如何,这是一个想法。

I'm not sure it's a good solution in general, as it may be overly restrictive. I haven't really thought it through very carefully. But anyway, it's an idea.

从<一个href=\"http://download.oracle.com/javase/1.4.2/docs/api/java/text/DateFormat.html#parse%28java.lang.String%29\"相对=nofollow> DateFormat.parse 的文件(这是由的SimpleDateFormat ):

该方法可以不使用整个文本
  给定的字符串。

The method may not use the entire text of the given string.

另外,从抛出同样的方法部分:

Also, from the "Throws" section of the same method:

ParseException的 - 如果的开始
  指定字符串
不能被解析。

ParseException - if the beginning of the specified string cannot be parsed.

注意这个:如果我实例化一个的SimpleDateFormat 字符串YYYY-MM-DD,那么它​​会成功解析字符串2011-05-12abcdefg:

Notice this: if I instantiate a SimpleDateFormat with the string "yyyy-MM-dd", then it will successfully parse the string "2011-05-12abcdefg":

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(format.parse("2011-05-12abcdefg"));

以上输出(在我的机器上):

The above outputs (on my machine):

Thu May 12 00:00:00 CDT 2011

所以基本上它只是解析的一样,因为它可以

事实上,这是行为,甚至与 setLenient(假),确实有点怪我。

The fact that this is the behavior, even with setLenient(false), is indeed a bit strange to me.

这篇关于SimpleDateFormat的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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