Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calculate date

 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get how to find it using GregorianCalendar. Anybody knows?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, for correcting me.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic