aspose file tools*
The moose likes Java in General and the fly likes Wants to add days in Date.. please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Wants to add days in Date.. please help" Watch "Wants to add days in Date.. please help" New topic
Author

Wants to add days in Date.. please help

Qasim Shabbir
Greenhorn

Joined: May 12, 2002
Posts: 16
hi all.
i want to add some days on date but its not giving me perfect result i m adding days on date.. my code is written below, the problem is whn counter value reaches to 25 it reverse the month... i do not understand y it happens. plz help me in that regards..

import java.util.Date;
public class Test{
public static void main(String ar[]){
Date date=new Date("10/1/2002");
System.out.println(date);
for (int i=1;i<30;i++){
System.out.println("Value to be Multi : "+i);
long ldt=date.getTime()+(1000*60*60*24)*i;//add one day
System.out.println(ldt);
System.out.println(new Date(ldt));
}
}
}
thankyou all in advance.
Qasim Shabbir
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
check out the Calendar class. It's pretty simple.
sandeep menon
Greenhorn

Joined: Oct 16, 2002
Posts: 3
use the add method in the calendar class and specify the field.
Volodymyr Shram
Ranch Hand

Joined: Oct 17, 2002
Posts: 47
Originally posted by Vin Kris:
check out the Calendar class. It's pretty simple.

Try out:
java.util.Date startDate = new Date("10/1/2002");
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
for (int i=1;i<30;i++){
System.out.println("Value to be Multi : "+i);
calendar.set(calendar.YEAR, calendar.MONTH, calendar.DAY_OF_MONTH + i);//add one day
System.out.println(calendar.geTime);
}
Regards, Scar.
Volodymyr Shram
Ranch Hand

Joined: Oct 17, 2002
Posts: 47
Originally posted by Scar:

calendar.set(calendar.YEAR, calendar.MONTH, calendar.DAY_OF_MONTH + i);//add one day

Or better use
calendar.add(calendar.DATE, 1);//add one day
istead my previous long and ugly line
With best wishes, Scar.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Scar,

Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
(Do I sound like a broken record ??? - sigh)
Thanks,
Cindy


"JavaRanch, where the deer and the Certified play" - David O'Meara
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Scar's answer is correct. Here is a fully implemented code:
Q. How to do arithmetic in Calendar class?
Another interesting one just follow it:
Q. How to calcucate the date difference between 09-01-2002 23:59:59 and 09-02-2002 00:00:01??
Think what should be the answer, please, before you go there.
[ October 21, 2002: Message edited by: Roseanne Zhang ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Wants to add days in Date.. please help