• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wants to add days in Date.. please help

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out the Calendar class. It's pretty simple.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use the add method in the calendar class and specify the field.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic