Neil said that "example : If I am given 13 March 2003, I must return a date exactly 90 days after the 1st April 2003".
Since some months are not 30 days, using add(Calendar.MONTH, 4) is not accurate here(it returns July 1, 2003 which
is the 91 days after April 1, 2003). Instead, we ought to use Calendar.DAY_OF_YEAR, the following is the sample
code:


import java.util.Calendar;

public class calTest{
   public static void main(String[] args) {
       Calendar cal = Calendar.getInstance();
       cal.set(2003, 02, 13, 00, 00, 00); //set date to 2003/3/13 00:00:00
       System.out.println("Initial Calendar: "+cal.getTime());

       cal.set(Calendar.DAY_OF_MONTH, 1); //First day of the Month
       cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1); // Next month

System.out.println("Calendar.DAY_OF_YEAR of "+cal.getTime()+": "+cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR)+ 90); //90 days after the first day of next month
System.out.println("Calendar.DAY_OF_YEAR of 90 days later: "+cal.get(Calendar.DAY_OF_YEAR));


       System.out.println("Final Calendar: "+cal.getTime());
   }
}

Results:
Initial Calendar: Thu Mar 13 00:00:00 EST 2003
Calendar.DAY_OF_YEAR of Tue Apr 01 00:00:00 EST 2003: 91
Calendar.DAY_OF_YEAR of 90 days later: 181
Final Calendar: Mon Jun 30 00:00:00 EDT 2003

-Daniel

Henri Yandell wrote:

Something like:

Date date = .. your date ..
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.MONTH, 4);

Hen

On Fri, 11 Jul 2003, neil wrote:



Hi, I've been experimenting with a couple of classes from the java.util
package, but I havent been able to solve my problem.

If I am given a date, and I want to return a date representing 90 days after
the first of the next month, what should I do?

example : If I am given 13 March 2003, I must return a date exactly 90 days
after the 1st April 2003







_______________________________________________ Juglist mailing list [EMAIL PROTECTED] http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to