aspose file tools*
The moose likes Java in General and the fly likes How to find number of days remains for next birthday Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to find number of days remains for next birthday" Watch "How to find number of days remains for next birthday" New topic
Author

How to find number of days remains for next birthday

Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
Hello All,

I am practicing with JSP and Bean. My home.jsp has form which takes three input first name, last name and birth day from user which calls another jsp to sets all bean properties and also call java class to calculate number of days remains for user's next birth day. I am not getting How can I calculate? Can any one guide me?

Thanking You,
Kasparov
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Create a Calendar object for today. In a loop, add one day until it reaches the desired birthday date, and increment a counter each time.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Kasparov Patel wrote:I am practicing with JSP and Bean. My home.jsp has form which takes three input first name, last name and birth day from user which calls another jsp to sets all bean properties and also call java class to calculate number of days remains for user's next birth day. I am not getting How can I calculate? Can any one guide me?

The "mathematical" alternative to Jeff's suggestion is to set up a java.util.Calendar object for today (the default) and the birthday in question and divide the difference in their getTimeInMillis() values by 86400000 (the number of milliseconds in a day). Quicker, but also trickier, because Calendars include the time of day, which you probably don't know for the person in question.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 190

The loop method would work but doesn't seem very elegant. Another approach would be to look at the calendar DAY_OF_YEAR for: the original date, the last day of the year, and the date one year later. Then some simple math should give you what you're looking for.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Winston Gutkowski wrote:
Kasparov Patel wrote:I am practicing with JSP and Bean. My home.jsp has form which takes three input first name, last name and birth day from user which calls another jsp to sets all bean properties and also call java class to calculate number of days remains for user's next birth day. I am not getting How can I calculate? Can any one guide me?

The "mathematical" alternative to Jeff's suggestion is to set up a java.util.Calendar object for today (the default) and the birthday in question and divide the difference in their getTimeInMillis() values by 86400000 (the number of milliseconds in a day). Quicker, but also trickier, because Calendars include the time of day, which you probably don't know for the person in question.

Winston


Why not just get( Calendar.DAY_OF_YEAR ) for both dates and perform a simple subtraction?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Dennis Deems wrote:Why not just get( Calendar.DAY_OF_YEAR ) for both dates and perform a simple subtraction?

Because it ain't quite so simple if you're not in the same year ([edit] or if you are and there's a 29th of Feb in between).

Personally, on the assumption that time of birth isn't known (ie, 00:00:00), I like my version best. Leave it up to the class, I say.

Winston
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
My code is as follow



I am confused how to do? Can you help me?
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509
In addition to what you have done,



Why 1970 is hardcode ? Look at the Calendar class documentation.

(Javadoc tag fails for java.lang.Calendar keyword. )
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Kasparov Patel wrote:I am practicing with JSP and Bean. My home.jsp has form which takes three input first name, last name and birth day from user which calls another jsp to sets all bean properties and also call java class to calculate number of days remains for user's next birth day. I am not getting How can I calculate? Can any one guide me?

The "mathematical" alternative to Jeff's suggestion is to set up a java.util.Calendar object for today (the default) and the birthday in question and divide the difference in their getTimeInMillis() values by 86400000 (the number of milliseconds in a day). Quicker, but also trickier, because Calendars include the time of day, which you probably don't know for the person in question.

Winston


I don't like that approach because a leap second could throw it off by one. Unlikely, but still makes it messy if you want to make it correct. (Although my solution has a similar issue with leap years. I just seem to remember there being problems with doing day math by simply dividing by millis-per-day.)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dennis Deems wrote:
Why not just get( Calendar.DAY_OF_YEAR ) for both dates and perform a simple subtraction?


I didn't even think of that. I just took the general approach for finding the number of days between two arbitrary dates and trimmed it down.

You'd have to essentially do a mod operation, in case your birthday is sooner in the year than today's date, and you'd have to account for leap years specially (as you would with my approach). Without reading the Calendar docs, I don't know if there are any other gotchas with this one.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 509
Jeff Verdegan wrote: and you'd have to account for leap years specially (as you would with my approach). Without reading the Calendar docs, I don't know if there are any other gotchas with this one.


I had the same problem when I used to code in VB. Isn't there any full-proof solution available? Apache Commons? Why don't they just provide a utility method built into Calendar class? Because this is too general problem.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Saurabh Pillai wrote:
Jeff Verdegan wrote: and you'd have to account for leap years specially (as you would with my approach). Without reading the Calendar docs, I don't know if there are any other gotchas with this one.


I had the same problem when I used to code in VB. Isn't there any full-proof solution available? Apache Commons? Why don't they just provide a utility method built into Calendar class? Because this is too general problem.


Java's build-in date/time handling has been known for years to be horrible for dealing with durations. The support just isn't there. The most commonly suggested alternative I've heard is Joda. I haven't used it myself, but I think it has some decent duration handling, and possibly concepts like dates without times and times without dates. It was supposed to be adapted for inclusion into the core API for Java 7, but it didn't make it for some reason. Now I've heard it's slated to be put into Java 8 in some form or other.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Saurabh Pillai wrote:I had the same problem when I used to code in VB. Isn't there any full-proof solution available? Apache Commons? Why don't they just provide a utility method built into Calendar class? Because this is too general problem.

I have excellent experience with the Joda time library. It is actually so good that it has been (with some modifications) included in JDK7. Getting the number of days between two dates is quite easy with it, and it handles daylight-saving-time and leap years correctly. I can't imagine our project (which is quite intensive on date-time arithmetic) to succeed without it.

Edit: I'm not on JDK7 yet, so I missed that it wasn't included, as Jeff states.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Jeff Verdegan wrote:I don't like that approach because a leap second could throw it off by one. Unlikely, but still makes it messy if you want to make it correct. (Although my solution has a similar issue with leap years. I just seem to remember there being problems with doing day math by simply dividing by millis-per-day.)

I suppose it might depend what you're referring to, but the fact is that leap seconds are simply ignored by Unix and Java (and any timing system that uses Unix-y clocks); except (I believe, but couldn't swear to it) for a display that happens to occur in a leap second (not so sure about maths). Otherwise, as far as I know, everything works as though all days have 86400 seconds. I f you know any different, do let me know.

@Patel: (and based on your code) My suggestion for would be something like:although I think the birthday() method could be removed altogether with a bit of smarts.

Winston


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Winston Gutkowski wrote:
Jeff Verdegan wrote:I don't like that approach because a leap second could throw it off by one. Unlikely, but still makes it messy if you want to make it correct. (Although my solution has a similar issue with leap years. I just seem to remember there being problems with doing day math by simply dividing by millis-per-day.)

I suppose it might depend what you're referring to, but the fact is that leap seconds are simply ignored by Unix and Java (and any timing system that uses Unix-y clocks); except (I believe, but couldn't swear to it) for a display that happens to occur in a leap second (not so sure about maths). Otherwise, as far as I know, everything works as though all days have 86400 seconds. I f you know any different, do let me know.


No, I don't know any different. I just have a vague recollection of learning that "calculating day deltas by dividing millis by 86,400,000 is not reliable" for some reason or other. I don't recall where I heard it, so I can't even attest to the authority of the source. It's just one of those things where I seem to remember being convinced at the time, and so it has stuck in my mind.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Winston Gutkowski wrote:
Dennis Deems wrote:Why not just get( Calendar.DAY_OF_YEAR ) for both dates and perform a simple subtraction?

Because it ain't quite so simple if you're not in the same year ([edit] or if you are and there's a 29th of Feb in between).

Personally, on the assumption that time of birth isn't known (ie, 00:00:00), I like my version best. Leave it up to the class, I say.

Winston


I really don't understand why someone would want the extra labor of converting time in milliseconds to days when Java Calendar gives you the number of days for free. Yes, it really is quite so simple. Java's calendar is smart enough to know whether there are 29 days in February. As for the problem of not being in the same year, in that case the result of the subtraction will be negative. Then you can just add the total number of days in the year you started from.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Jeff Verdegan wrote:"calculating day deltas by dividing millis by 86,400,000 is not reliable"


This is because of daylight saving time. Most days have 24 hours, but days when daylight saving time starts have only 23 and days when daylight saving time ends have 25. So if you're unlucky it's possible to use the "divide by 86,400,000" rule and end up with a number of milliseconds which represents, say, 71 hours instead of 72 hours. And then because you did an integer (or long) division, truncation to the next lower integer produces 2 days instead of 3.

Fun facts:

India doesn't use daylight saving time.

There's a timezone whose offset is not 1 hour.
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
Thanks for all to guide me. I tried both way, but not succeed. Please check my following modified code

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dennis Deems wrote:
Winston Gutkowski wrote:
Dennis Deems wrote:Why not just get( Calendar.DAY_OF_YEAR ) for both dates and perform a simple subtraction?

Because it ain't quite so simple if you're not in the same year ([edit] or if you are and there's a 29th of Feb in between).

Personally, on the assumption that time of birth isn't known (ie, 00:00:00), I like my version best. Leave it up to the class, I say.

Winston


I really don't understand why someone would want the extra labor of converting time in milliseconds to days when Java Calendar gives you the number of days for free. Yes, it really is quite so simple. Java's calendar is smart enough to know whether there are 29 days in February. As for the problem of not being in the same year, in that case the result of the subtraction will be negative. Then you can just add the total number of days in the year you started from.


I'd say the "extra labor" of dividing by 86400000 is not worth quibbling over. It's a hair's breadth away from being the same amount of code in either case. As long as they both work correctly, go with the one that you consider easiest or clearest. I don't know of any significant objective argument in favor of one or the other.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:
Jeff Verdegan wrote:"calculating day deltas by dividing millis by 86,400,000 is not reliable"


This is because of daylight saving time. Most days have 24 hours, but days when daylight saving time starts have only 23 and days when daylight saving time ends have 25. So if you're unlucky it's possible to use the "divide by 86,400,000" rule and end up with a number of milliseconds which represents, say, 71 hours instead of 72 hours. And then because you did an integer (or long) division, truncation to the next lower integer produces 2 days instead of 3.


I don't think that matters here, but my brain is rapidly devolving into Friday afternoon mode, so I may have missed something.

The reason I don't think it matters is this: When we set our clocks ahead one hour for DST, the result of System.currentTimeMillis() does NOT (to my knowledge) jump ahead by 60 * 60 * 1000. Rather, all that happens is that we have now moved from, e.g., EST to EDT, and so instead of displaying that millis as "02:00 EST" we now display the same millis as "03:00 EDT". Same applies in reverse for falling back off of DST.

Now, the only place where I could see where it would make a difference is for that hour in the fall that we traverse twice. If we start by saying "I was born on Oct. whatever at 02:30 a.m.", and we don't specify a TZ so Java has to infer one based on our current system TZ of, say, EDT, then does it take that 02:30 as being 02:30 EDT (as opposed to EST) because we are, at this moment, in EDT, and therefore if we did the same thing in the winter time would it take it as EST because that's the current part of the year we're in? Or does it always assume the DST (or non-DST) version of our TZ? Depending on how that gets handled, it might pick the wrong 02:30 for that day, and, yeah, we could be off by +/- an hour which could cascade to higher-order units.

HOWEVER, having said all that, if I'm calculating "days until some date", unless I'm told otherwise, I'm going to assume we're talking about dates without time, which core Java doesn't support well, but which can be simulated for this use case by simply zeroing out hours, minutes, seconds, and millis for our start and end dates. Midnight to midnight.


Fun facts:

India doesn't use daylight saving time.



Neither do parts of Indiana. Hmmm... similar names, similar attitudes toward DST, and you never see them together. Coincidence?


There's a timezone whose offset is not 1 hour.


Aren't there several?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kasparov Patel wrote:
It's always giving me 456. Can anyone correct my code either directly using day or time?


People aren't here to fix your code for you. We come here to help you figure out how to fix it yourself.

You first step at this point should be to print out all the intermediate values and calculation results and compare that to what you get when you work it through by hand. Where the two diverge, you probably have the source of your problem.
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
I tried using earlier.get( Calendar.DAY_OF_YEAR ) and later.get( Calendar.DAY_OF_YEAR ), but getting wrong value. The final result is 0.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kasparov Patel wrote:I tried using earlier.get( Calendar.DAY_OF_YEAR ) and later.get( Calendar.DAY_OF_YEAR ), but getting wrong value. The final result is 0.


Then you did something wrong. Since we can't see what your code looked like when you tried that, we can't tell you what you did wrong.

However, my previous advice applies in that case as well: Use lots of println() calls to see what's happening each step of the way.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Jeff Verdegan wrote:

There's a timezone whose offset is not 1 hour.


Aren't there several?


No, only one. If you run the small Java program to go through all the time zones and find zones which observe DST but the time difference isn't 1 hour, you appear to find two, but they are different names for the same time zone.
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
Here is my code using get(calendar.DAY_OF_YEAR)

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:
Jeff Verdegan wrote:

There's a timezone whose offset is not 1 hour.


Aren't there several?


No, only one. If you run the small Java program to go through all the time zones and find zones which observe DST but the time difference isn't 1 hour, you appear to find two, but they are different names for the same time zone.


Ah, okay, only 1 in Java. I was just thinking of what's in use in the world, didn't think about whether Java would recognize all of them. This page lists several 30 minute offsets and even a couple of 45 minute offsets: http://en.wikipedia.org/wiki/List_of_time_zones_by_UTC_offset
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kasparov Patel wrote:Here is my code using get(calendar.DAY_OF_YEAR)


Lovely.

Only I'm not going to read all of it and try to figure out the problem. Please follow the advice that I have no given you twice and put in a bunch of print statements. Show that code, with its output, and indicate where the output is different from what you expected.
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
I already debug using print statement. System.out.println(earlier.get( Calendar.DAY_OF_YEAR )); showing me 76 which should be 107 and System.out.println(later.get( Calendar.DAY_OF_YEAR )); showing 166 instead of 136. That's why I changed my code to convert in milliseconds, but neither one is working.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

Jeff Verdegan wrote:Ah, okay, only 1 in Java. I was just thinking of what's in use in the world, didn't think about whether Java would recognize all of them. This page lists several 30 minute offsets and even a couple of 45 minute offsets: http://en.wikipedia.org/wiki/List_of_time_zones_by_UTC_offset


Right, but when I said "offset" I actually meant the amount of time you set your clock back or forward when daylight saving time ends or starts. Not the time zone's offset from UTC. It isn't always one hour.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Paul Clapham wrote:
Jeff Verdegan wrote:Ah, okay, only 1 in Java. I was just thinking of what's in use in the world, didn't think about whether Java would recognize all of them. This page lists several 30 minute offsets and even a couple of 45 minute offsets: http://en.wikipedia.org/wiki/List_of_time_zones_by_UTC_offset


Right, but when I said "offset" I actually meant the amount of time you set your clock back or forward when daylight saving time ends or starts. Not the time zone's offset from UTC. It isn't always one hour.


D'OH!

(Friday and all...)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kasparov Patel wrote:I already debug using print statement. System.out.println(earlier.get( Calendar.DAY_OF_YEAR )); showing me 76 which should be 107 and System.out.println(later.get( Calendar.DAY_OF_YEAR )); showing 166 instead of 136. That's why I changed my code to convert in milliseconds, but neither one is working.


Sorry, but I don't want a description.

I want to see the code with the print statements in it.
I want to see the output.
I want to see your comments on where the output differed from what you expected.
I want to see all that in one post, so I don't have to jump around and piece things together.

I'm leaving now, for Friday night dinner and drinks, but maybe somebody else will still be around to look at your code. If not, I'll take a look tomorrow.

Good luck!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Jeff Verdegan wrote:
Paul Clapham wrote:This is because of daylight saving time. Most days have 24 hours, but days when daylight saving time...
I don't think that matters here, but my brain is rapidly devolving into Friday afternoon mode, so I may have missed something.

Don't think so. Fact is, like it or not, my method let's the class do the work (millisTime doesn't worry about things like DST); but I can understand why it may not be everybody's choice. Personally, my second would be the one that you suggested in your very first post.

Winston
Kasparov Patel
Ranch Hand

Joined: Jan 23, 2012
Posts: 40
Thanks to all, I have figured out.
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
Kasparov Patel wrote:Thanks to all, I have figured out.


Hey, Can you please send this solution to my id <removed>, I need this solution...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Rajesh, you should UseTheForumNotEmail. Also, we are NotACodeMill. You should try to come up with something yourself, or you won't learn anything.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to find number of days remains for next birthday