Java Calendar Class

Hi,

I currently have a method that takes the current time and date and returns the date 3 weeks later, I want to change this to 10 calendar days later.

public Date createFirstDateFromDirectDebitDate()
      {
            Calendar firstAllowable = Calendar.getInstance();
            
            /* set calendar value to 3 weeks time */
            firstAllowable.setTime(new Date());
            firstAllowable.add(Calendar.WEEK_OF_YEAR, 3);
            
            return firstAllowable.getTime();
      }

I tried using DAY_OF_YEAR but it seems to only give me 10 working days later

firstAllowable.add(Calendar.DAY_OF_YEAR, 10);

Can anyone tell what im doing wrong??

Thanks
Suzy
fynessAsked:
Who is Participating?
 
thomasbauConnect With a Mentor Commented:
This has to work


  public Date createFirstDateFromDirectDebitDate()
     {
          Calendar firstAllowable = Calendar.getInstance();
          Date date = new Date();
          /*
           *Add 10 days*/
          long offset = date.getTime();
          long milPerDay = 24*60*60*1000;
          offset += 10 * milPerDay;
          date.setTime(offset);
         
          /*
           *Set the new date in the calendar*/
          firstAllowable.setTime(date);
          /* set calendar value to 3 weeks time */
          //firstAllowable.setTime(new Date());
          //firstAllowable.add(Calendar.WEEK_OF_YEAR, 3);
         
          return firstAllowable.getTime();
     }

0
 
CEHJCommented:
TRY

firstAllowable.add(Calendar.DATE, 10);
0
 
TimYatesCommented:
firstAllowable.add(Calendar.DAY_OF_YEAR, 10);

should work
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
girionisCommented:
> firstAllowable.add(Calendar.DAY_OF_YEAR, 10);

It should do. Maybe you are mistaken, can you double check?
0
 
CEHJCommented:
What you have should work, although my version needs less typing ;-)

>>firstAllowable.setTime(new Date());

is redundant btw as Calendar is already initialized with the current date
0
 
TimYatesCommented:
> although my version needs less typing ;-)

LOL ;-)
0
 
fynessAuthor Commented:
Thanks for your replys but still no luck. I've double checked and am still having trouble, I even tried changing DAY_OF_WEEK from 10 to 6 and its the same problem. I also tried using Calendar.DATE and still no joy!

There must be another problem that i dont see yet

Thanks again for your help!
0
 
TimYatesCommented:
can you post more of your code?

This *does* work...
0
 
CEHJCommented:
>>There must be another problem that i dont see yet

It will probably amount to not using the Date that you returned from your method ;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Are you using two firstAllowable.add () statements anywhere?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.