Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wants to add days in Date.. please help

 
Qasim Shabbir
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out the Calendar class. It's pretty simple.
 
sandeep menon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the add method in the calendar class and specify the field.
 
Volodymyr Shram
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic