Solved

Java Calendar Class

Posted on 2004-08-10
10
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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:Mayank S
ID: 11791164
Are you using two firstAllowable.add () statements anywhere?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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