• Post Reply Bookmark Topic Watch Topic
  • New Topic

using Date and Calendar classes  RSS feed

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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

 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Paul and Sidd.

That was extremely helpful.

Much appreciated.

Ricky
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!