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

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

public boolean before(Object when) {spring

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

    }
函数

可是仍是没留意到 when instanceof Calendar 这条code。最后决定使用最底层的逻辑:比较getTimeInMillis 。带回家再找缘由。测试

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

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

    @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));
    }
orm

   正确状况下赢输出:xml

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

可是实际状况确实ip

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环境很重要啊。。。   

相关文章
相关标签/搜索