Solved

Java Calendar Class

Posted on 2004-08-10
10
209 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:fyness
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11761913
TRY

firstAllowable.add(Calendar.DATE, 10);
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11761920
firstAllowable.add(Calendar.DAY_OF_YEAR, 10);

should work
0
 
LVL 35

Expert Comment

by:girionis
ID: 11761941
> firstAllowable.add(Calendar.DAY_OF_YEAR, 10);

It should do. Maybe you are mistaken, can you double check?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11761989
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11761991
> although my version needs less typing ;-)

LOL ;-)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:fyness
ID: 11762085
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
 
LVL 35

Expert Comment

by:TimYates
ID: 11762119
can you post more of your code?

This *does* work...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11762194
>>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
 

Accepted Solution

by:
thomasbau earned 125 total points
ID: 11785661
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11791164
Are you using two firstAllowable.add () statements anywhere?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now