请教一个String转换成日期的异常处理问题

gdzcxxzx 2017-12-29 04:09:48
大家好!

想请教一个问题。一个String转换成日期格式yyyy-mm-dd。但为什么输入2016-01-0222,2016-01-44785,2016-01-20Er这种都没有提示异常,反而是2016-01-ER,sdfsdfsdf这些的会提示异常。

try {
HashMap paramMap = (HashMap) requestMap.get("paramMap");
String newValue = (String) paramMap.get("New Value");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
if(!newValue.equals("")){
Date tDate = sdf.parse(newValue);
}
} catch (Exception e) {
MqlUtil.mqlCommand(context, "notice '" + strMessage + "'");
return 1;
}
...全文
693 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
领悟97 2018-01-14
  • 打赏
  • 举报
回复
日期现在大部分都是控件输入,不会让用户输入,没必要纠结这些。
yannsPeng 2017-12-29
  • 打赏
  • 举报
回复
引用 3 楼 gdzcxxzx 的回复:
主要是这样的,我的属性是string的,本来是异常报错就不让保存,现在2016-01-20Er这种都可以保存得上,这种就很怪了。能否如何处理呢?
http://blog.csdn.net/love_legain/article/details/53617783 http://blog.csdn.net/love_legain/article/details/53679442 既然不能强校验字符串合法性要么你前台验证,要么后台正则验证。
oyljerry 2017-12-29
  • 打赏
  • 举报
回复
引用 3 楼 gdzcxxzx 的回复:
主要是这样的,我的属性是string的,本来是异常报错就不让保存,现在2016-01-20Er这种都可以保存得上,这种就很怪了。能否如何处理呢?
直接用正则表达式等判断一下格式
\\d{4}-\\d{2}-\\d{2}
gdzcxxzx 2017-12-29
  • 打赏
  • 举报
回复
主要是这样的,我的属性是string的,本来是异常报错就不让保存,现在2016-01-20Er这种都可以保存得上,这种就很怪了。能否如何处理呢?
yannsPeng 2017-12-29
  • 打赏
  • 举报
回复
2016-01-0222 结果是222天往前进位 2016-01-44785 同上 2016-01-20Er 明显过滤掉了Er 2016-01-ER 都没有数字怎么不报异常怎么玩 没什么太大必要纠结源码怎么写。
yannsPeng 2017-12-29
  • 打赏
  • 举报
回复
yyyy-mm-dd 你自己定义的。
    public Date parse(String text, ParsePosition pos)
    {
        checkNegativeNumberExpression();

        int start = pos.index;
        int oldStart = start;
        int textLength = text.length();

        boolean[] ambiguousYear = {false};

        CalendarBuilder calb = new CalendarBuilder();

        for (int i = 0; i < compiledPattern.length; ) {
            int tag = compiledPattern[i] >>> 8;
            int count = compiledPattern[i++] & 0xff;
            if (count == 255) {
                count = compiledPattern[i++] << 16;
                count |= compiledPattern[i++];
            }

            switch (tag) {
            case TAG_QUOTE_ASCII_CHAR:
                if (start >= textLength || text.charAt(start) != (char)count) {
                    pos.index = oldStart;
                    pos.errorIndex = start;
                    return null;
                }
                start++;
                break;

            case TAG_QUOTE_CHARS:
                while (count-- > 0) {
                    if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
                        pos.index = oldStart;
                        pos.errorIndex = start;
                        return null;
                    }
                    start++;
                }
                break;

            default:
                // Peek the next pattern to determine if we need to
                // obey the number of pattern letters for
                // parsing. It's required when parsing contiguous
                // digit text (e.g., "20010704") with a pattern which
                // has no delimiters between fields, like "yyyyMMdd".
                boolean obeyCount = false;

                // In Arabic, a minus sign for a negative number is put after
                // the number. Even in another locale, a minus sign can be
                // put after a number using DateFormat.setNumberFormat().
                // If both the minus sign and the field-delimiter are '-',
                // subParse() needs to determine whether a '-' after a number
                // in the given text is a delimiter or is a minus sign for the
                // preceding number. We give subParse() a clue based on the
                // information in compiledPattern.
                boolean useFollowingMinusSignAsDelimiter = false;

                if (i < compiledPattern.length) {
                    int nextTag = compiledPattern[i] >>> 8;
                    if (!(nextTag == TAG_QUOTE_ASCII_CHAR ||
                          nextTag == TAG_QUOTE_CHARS)) {
                        obeyCount = true;
                    }

                    if (hasFollowingMinusSign &&
                        (nextTag == TAG_QUOTE_ASCII_CHAR ||
                         nextTag == TAG_QUOTE_CHARS)) {
                        int c;
                        if (nextTag == TAG_QUOTE_ASCII_CHAR) {
                            c = compiledPattern[i] & 0xff;
                        } else {
                            c = compiledPattern[i+1];
                        }

                        if (c == minusSign) {
                            useFollowingMinusSignAsDelimiter = true;
                        }
                    }
                }
                start = subParse(text, start, tag, count, obeyCount,
                                 ambiguousYear, pos,
                                 useFollowingMinusSignAsDelimiter, calb);
                if (start < 0) {
                    pos.index = oldStart;
                    return null;
                }
            }
        }

        // At this point the fields of Calendar have been set.  Calendar
        // will fill in default values for missing fields when the time
        // is computed.

        pos.index = start;

        Date parsedDate;
        try {
            parsedDate = calb.establish(calendar).getTime();
            // If the year value is ambiguous,
            // then the two-digit year == the default start year
            if (ambiguousYear[0]) {
                if (parsedDate.before(defaultCenturyStart)) {
                    parsedDate = calb.addYear(100).establish(calendar).getTime();
                }
            }
        }
        // An IllegalArgumentException will be thrown by Calendar.getTime()
        // if any fields are out of range, e.g., MONTH == 17.
        catch (IllegalArgumentException e) {
            pos.errorIndex = start;
            pos.index = oldStart;
            return null;
        }

        return parsedDate;
    }

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧