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?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
Dennis Deems wrote:Why not just get( Calendar.DAY_OF_YEAR ) for both dates and perform a simple subtraction?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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
Dennis Deems wrote:
Why not just get( Calendar.DAY_OF_YEAR ) for both dates and perform a simple subtraction?
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.
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.
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.
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.)
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
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
Jeff Verdegan wrote:"calculating day deltas by dividing millis by 86,400,000 is not reliable"
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.
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.
Fun facts:
India doesn't use daylight saving time.
There's a timezone whose offset is not 1 hour.
Kasparov Patel wrote:
It's always giving me 456. Can anyone correct my code either directly using day or time?
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.
Jeff Verdegan wrote:
There's a timezone whose offset is not 1 hour.
Aren't there several?
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.
Kasparov Patel wrote:Here is my code using get(calendar.DAY_OF_YEAR)
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
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.
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.
Jeff Verdegan wrote:
I don't think that matters here, but my brain is rapidly devolving into Friday afternoon mode, so I may have missed something.Paul Clapham wrote:This is because of daylight saving time. Most days have 24 hours, but days when daylight saving time...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Kasparov Patel wrote:Thanks to all, I have figured out.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Consider Paul's rocket mass heater. |