我是这么做的
long timeS = N*365 * 24 * 60 * 60 * 1000; //用毫秒表示N年的时间
Date s = new Date(System.currentTimeMillis() - timeS);//用当前毫秒时间减去中间差就是N年前
的今天不过好像有问题
40*365 * 24 * 60 * 60 * 1000的值怎么是负的 -1280385024
而当前System.currentTimeMillis() 的值1139487161359 请教,把时间向前向后调整N年,N月,N天的办法。
谢谢

解决方案 »

  1.   

    一年不一定就是365天啊,楼主这么做本来就不对的
    我看还是取得当前的时间比如2006-02-09,拿当前年份-N年(月、日同理,假如N=3)
    那就是2003-02-09
    再用如下方法转换成DATE型的
    Date m_endTime = new java.text.SimpleDateFormat("yyyy-MM-dd").parse("2003-02-09");
      

  2.   

    我用Calendar 计算的,可以参考.
       /**
         * Get the date of monday in this week
         * 
         * @return yyyy-MM-dd
         */
        public static String getMondayOfThisWeek() {
            String strTemp = "";
            Calendar c = Calendar.getInstance();
            int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
            if (dayofweek == 0)
                dayofweek = 7;
            c.add(Calendar.DATE, -dayofweek + 1);
            strTemp = c.get(1) + "-";
            if (c.get(2) + 1 < 10)
                strTemp += "0";
            strTemp = strTemp + (c.get(2) + 1) + "-";
            if (c.get(5) < 10)
                strTemp += "0";
            strTemp += c.get(5);
            return strTemp;
        }    /**
         * Get the date of sunday in this week
         * 
         * @return yyyy-MM-dd
         */
        public static String getSundayOfThisWeek() {
            String strTemp = "";
            Calendar c = Calendar.getInstance();
            int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
            if (dayofweek == 0)
                dayofweek = 7;
            c.add(Calendar.DATE, -dayofweek + 7);
            strTemp = c.get(1) + "-";
            if (c.get(2) + 1 < 10)
                strTemp += "0";
            strTemp = strTemp + (c.get(2) + 1) + "-";
            if (c.get(5) < 10)
                strTemp += "0";
            strTemp += c.get(5);
            return strTemp;
        }    /**
         * Get the date of AfterDays
         * 
         * @param day
         * @return yyyy-MM-dd
         */
        public static String getTheDayAfterDays(int day) {
            String strTemp = "";
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DATE, day);
            strTemp = c.get(1) + "-";
            if (c.get(2) + 1 < 10)
                strTemp += "0";
            strTemp = strTemp + (c.get(2) + 1) + "-";
            if (c.get(5) < 10)
                strTemp += "0";
            strTemp += c.get(5);
            return strTemp;
        }