wood burning stoves*
The moose likes Java in General and the fly likes Calculate date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calculate date " Watch "Calculate date " New topic
Author

Calculate date

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

I want to print a date of one year later. I tried to write the program but it will not print day properly(it calculate year and month correctly).


but nextDate is not giving me the exam date of nect year . out put is



Here year is correct i.e 2011 but month and date is not what I want.(I want month as May and date as 23)

Thanks in advance


Life is easy because we write the source code.....
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

When I calculate nextDate as

then it prints correct date after one year as


But why is it so? Is this right? or there is another option to calculate date of next year?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
You need to read the Date API. In particular, pay attention where they use the term "deprecated". On just about every method you use here. Read what they say about what you should do instead.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

I didn't get how to find it using GregorianCalendar. Anybody knows?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You already have a class that can do what you want. You want to add one year to the calendar object. Are you sure you can't find a method to do that?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Rob Prime wrote:You already have a class that can do what you want. You want to add one year to the calendar object. Are you sure you can't find a method to do that?

Um, in the code shown so far, he doesn't have a calendar; he has a Date. There are methods in Date that could conceivably be used here, but the API says they're deprecated, and we should use methods in Calendar or GregorianCalendar instead. So that's why he's looking into new classes like GregorianCalendar.

Pramod, can you show what you've tried with GregorianCalendar, or tell us what part of the API is confusing? Sadly, I know many parts of Java's date.time.calendar stuff can be confusing - we just need to focus on the particular part or parts you're having trouble with.

Googling things like "java GregorianCalendar examples" can also be helpful here.

Also I suggest paying careful attention to the difference between getDay() or get(Calendar.DAY) with getDate() or get(Calendar.DATE). I think this is actually the biggest problem with your original code.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Mike Simmons wrote:
Rob Prime wrote:You already have a class that can do what you want. You want to add one year to the calendar object. Are you sure you can't find a method to do that?

Um, in the code shown so far, he doesn't have a calendar; he has a Date. There are methods in Date that could conceivably be used here, but the API says they're deprecated, and we should use methods in Calendar or GregorianCalendar instead. So that's why he's looking into new classes like GregorianCalendar.

I know, he found that one:
Pramod P Deore wrote:I didn't get how to find it using GregorianCalendar. Anybody knows?

That was the class I was talking about.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
Hmm, OK. The "already" sounded to me like a reference to the earlier class found. As opposed to the one he just found.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hi, Rob and Mike Thanks for reply, I think now I got it. I had used now get(int ), roll(int,int) and compareTo method as follows


Once again Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Don't use 1, 2 and 5. For anyone reading your code, including yourself in a few weeks / months, they mean nothing. Use the constant names:
Like I said, 1, 2 and 5 mean nothing to me. Calendar.YEAR, Calendar.MONTH and Calendar.DAY_OF_MONTH make it clear what you're trying to do.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Thanks Rob, for correcting me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculate date