Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java Calendar Class

Posted on 2004-08-10
10
Medium Priority
?
218 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 375 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

971 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