Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

using Date and Calendar classes

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am a beginner in JSP and am working on this exercise:

Using Date and Calendar objects, put together a JSP page that displays the
number of days remaining until your next birthday. (Hint: the DAY_OF_YEAR
field will be useful here.)


I think I am getting the wrong result as the no. of days that I get when I use a calculator is 305. But I don't understand what I am doing wrong here.

Can someone please advise.

Thanks in advance.
Ricky

 
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As all the date calculations have nothing to do with JSP, I'm moving this to a more general forum.
 
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

calendar2.set(2008, 02, 17, 23, 59);

I expect you made the natural and extremely common assumption that month 2 was February. Well, not in Java. In Java the months are numbered starting from zero; however what you should really write there is
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ricky, Paul's right. The months are counted from 0 and not 1. Also, you haven't considered the possibility of the birthday occuring in the same year. You can add up the following.

 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul and Sidd.

That was extremely helpful.

Much appreciated.

Ricky
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

calendar2.set(2008, 02, 17, 23, 59);


Also, never start a numeric literal in Java with a zero, as you did with "02" here. If you do that, Java will interpret the number as an octal (not decimal) number. Try this and see what the compiler says:
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jesper Young:

Also, never start a numeric literal in Java with a zero, as you did with "02" here. If you do that, Java will interpret the number as an octal (not decimal) number. Try this and see what the compiler says:



Thanks Jasper. This kind of help makes me confident. I am a beginner programmer. And every extra bit of information is helpful.
Thanks again.
Ricky
 
reply
    Bookmark Topic Watch Topic
  • New Topic