/**
     * Get the interval date by specified start date and time
     * @param d the start date
     * @param time the interval time
     * @param isAfter count the after date,otherwise cout before date
     * @return
     */
    public static Date getIntervalTime(Date d,String time,boolean isAfter)
    {
        try
        {
            Calendar c = java.util.Calendar.getInstance();            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            Date dCount = sdf.parse(time);
            c.setTime(d);
            if(isAfter)
                c.add(Calendar.SECOND,getSecondByTime(time));
            else
                c.add(Calendar.SECOND,0-getSecondByTime(time));
            return c.getTime();
//System.out.println(dCount.getHours());
//System.out.println(dCount.getSeconds());
//System.out.println(dCount.getMinutes());
        }catch(java.text.ParseException e)
        {
            throw new MFUtilException("Translate date time faield:" + time);
        }
    }

解决方案 »

  1.   

    /**
         * Get the seconds by the specified HH:mm:ss
         *
         * @param time the specified format time
         *
         * @return the total seconds.
         */
        public static int getSecondByTime(String time)
        {
            StringTokenizer tok = new StringTokenizer(time, ":");        String HH = "";
            if(tok.hasMoreTokens())
            {
                HH = tok.nextToken();
            }        String mm = "";
            if(tok.hasMoreTokens())
            {
                mm = tok.nextToken();
            }        String ss = "";
            if(tok.hasMoreTokens())
            {
                ss = tok.nextToken();
            }        int iHH = 0;
            try{iHH = Integer.parseInt(HH);}
            catch(Exception e){}        int imm = 0;
            try
            {
                imm = Integer.parseInt(mm);
            }catch(Exception e){}        int iss = 0;
            try
            {
                iss = Integer.parseInt(ss);
            }catch(Exception e){}        int seconds = iHH * 3600 + imm * 60 + iss;
            return seconds;
        }
      

  2.   

    我记得Date的hour取值范围(0-11)
    你set差1个小时,再get就又差了1个小时。而且Date(String s) 、parse(String s) 这2函数为Deprecated.最好不要在程序中使用。
      

  3.   

    to dmhorse(dmhorse),你这个怎么调用?
    行么?
      

  4.   

    Date nextTowHour d = new Date(System.currentMillseconds() + 1000 * 3600 * 2);
      

  5.   

    Date date = new Date(new Date().getTime()+1000*3600*2); ------------------------------------------------------
               我们还年轻牛奶会有的奶牛也会有的 
                 可天天在 csdn 混这些会有吗 ??
      

  6.   

    在java2核心技术中有sun公司提供的Date 并且提供追加功能
      

  7.   

    在《java2核心技术》书中提供Date,可以实现追加功能