Discussions

Web tier: servlets, JSP, Web frameworks: How can I get the date of 30 days ago in JDK1.2.2?

  1. I used to use the following code to calculate the date of 30 days ago in JDK 1.1.8:

    java.sql.Date ToDate = new java.sql.Date(2001, 04, 23-30);

    However, the new java.sql.Date(int, int, int) is depreciated in JDK 1.2.2. Just wondering how do we calculate the date of 30 days ago? Any help would be appreciated. Thank you.

  2. The constructor you are trying to is deprecate since jkd1.1
    The only contructor now takes a long

    Try the following code sequence is a suggtested work-around


    java.util.Calendar date = java.util.Calendar.getInstance();
           // Set your initial date
    date.set(2001, 04, 30);

            /*
                  Then go back x days, where n is an int
                  To add x day ahead, use positive int
                  To substract x days , use negative int
           */
    date.add(Calendar.DATE,-30); // go back 30 days

           /*
             Similar methos exist for years, months, hours,
               minute, seconds respectively

    date.set(Calendar.YEAR, years);
    date.add(Calendar.MONTH, months);
    date.add(Calendar.HOUR, hours);
    date.add(Calendar.MINUTE, minutes);
    date.add(Calendar.SECOND, seconds);

             Where Variable years, months, minutes
              and seconds are int.
              Use positive values to advance, and negative
             values to turn back the calendar
              
             Please check the API for more info

         */


    Hope this helps,

     Kipre




  3. Kipre,

      Thank you for the advise. I will give it a try. Thank you.

    Janice