• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Calculate date

 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 5116
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't get how to find it using GregorianCalendar. Anybody knows?
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 5116
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 5116
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, for correcting me.
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic