• Spring Tool Suite(简称STS)针对SimpleDateFormat.pase函数的实参值不做检验,异常直接默认值之


          Spring Tool Suite(简称STS)是 Spring 团队开发的一款基于Eclipse的IDE,旨在简化开发Spring MVC 应用的流程。可以自动生成spring相关的配置文件。比如applicationContext.xml文件等。但是近来使用 Calendar日历类进行比较日期时,发现before、after函数不能输出预期的结果,于是逐一翻看Calendar源码:

    public boolean before(Object when) {

            return when instanceof Calendar
                && compareTo((Calendar)when) < 0;

        }

    但是还是没留意到 when instanceof Calendar 这条code。最后决定使用最底层的逻辑:比较getTimeInMillis 。带回家再找原因。

          后来翻看API文档才注意到当且仅当 when 是Calendar实例时才会返回true。

         但是这中间暴露了STS的一个bug。测试代码如:

        @Test
        public void testCalendar(){
            String pattern1 = "YYYY-MM-dd";
            String pattern2 = "yyyy-MM-dd";

            SimpleDateFormat format = new SimpleDateFormat(pattern1);
            Date theD = null ;
            try {
                theD = format.parse("2013-10-10");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar theC = Calendar.getInstance() ;
            theC.setTime(theD);
            Calendar now = Calendar.getInstance() ;
            System.out.println("theC : "+theC.getTime());
            System.out.println("theC : "+theC.getTimeInMillis());
            System.out.println("new : "+now.getTime());
            System.out.println("new : "+now.getTimeInMillis());
            System.out.println("new is before theC : "+now.before(theC));
        }

       正确情况下赢输出:

    theC : Wed Jan 10 00:10:00 CST 3
    theC : -62071948200000
    new : Tue Sep 10 22:22:15 CST 2013
    new : 1378822935807
    new is before theC : false

    但是实际情况确实

    theC : Sun Dec 30 00:00:00 CST 2012
    theC : 1356796800000
    new : Tue Sep 10 22:31:28 CST 2013
    new : 1378823488012
    new is before theC : false
    可以看到 theC变成了 2012年10月30号

        不论吧pase函数内的实参修改成何值最后输出的都是 10月30号。无果,之后更换IDE。打开传统的myEclipse,Ctrl+C复制黏贴代码。奇迹出现:

    直接抛出java.lang.IllegalArgumentException: Illegal pattern character 'Y',原来pattern1格式错误应为pattern2.但是万恶的STS竟然没有抛出任何异常,直接默认为了10月30号。

        看了成熟IDE环境很重要啊。。。   



  • 相关阅读:
    Ionic Cordova 环境配置window
    滚动条超出表格标题,表格标题固定
    逗号分隔查询
    sulin LuceneNet 搜索二
    LuceneNet 搜索一
    Sql 竖表转横表
    div 无缝滚动
    【bfs】【中等难度】wikioi3055 青铜莲花池
    【bfs】【中等难度】tyvj P1234
    【简单思考】noip模拟赛 NTR酋长
  • 原文地址:https://www.cnblogs.com/pangblog/p/3315343.html
Copyright © 2020-2023  润新知