Java Forum / General / May 2007
calculate day, hour , sec between dates
Alexandre Jaquet - 22 May 2007 14:20 GMT Hi,
I'm looking for a way to retrieve the day, hour and sec between two dates object
I know how to retrieve the number of days :
long daterange = dateEnd.getTime() - dateBegin.getTime(); long time = 1000*3600*24; //A day in milliseconds long day = daterange/time; auctionDurationRest = " jour : " + day;
Regards,
Alexandre
Eric Sosman - 22 May 2007 15:05 GMT Alexandre Jaquet wrote On 05/22/07 09:20,:
> Hi, > [quoted text clipped - 7 lines] > long day = daterange/time; > auctionDurationRest = " jour : " + day; Think about the value `daterange % time' or `daterange - day * time'. What would this value represent?
 Signature Eric.Sosman@sun.com
Alexandre Jaquet - 22 May 2007 15:16 GMT > Alexandre Jaquet wrote On 05/22/07 09:20,: > [quoted text clipped - 13 lines] > `daterange - day * time'. What would this value > represent? Hi Eric,
daterange % time could represent the long value rest of datarange (datarange minus number of days) if I understand well
Alexandre
Alexandre Jaquet - 22 May 2007 15:25 GMT >> Alexandre Jaquet wrote On 05/22/07 09:20,: >> [quoted text clipped - 20 lines] > > Alexandre I suppose hours are calculated :
long daterange = dateEnd.getTime() - dateBegin.getTime(); long time = 1000*3600*24; //A day in milliseconds long day = daterange/time; long hours = (daterange % time) / (1000*3600);
Do you think that's rigth ?
Regards,
Alexandre
Eric Sosman - 22 May 2007 16:32 GMT Alexandre Jaquet wrote On 05/22/07 10:25,:
>>>Alexandre Jaquet wrote On 05/22/07 09:20,: >>> [quoted text clipped - 29 lines] > > Do you think that's rigth ? Right. And you could use a similar method to calculate minutes, and seconds, and even milliseconds.
There is nothing mysterious about this process. Forget about Java for a moment, and imagine using a stopwatch to measure your time in a Marathon foot race. You click the watch when you start, and again when you cross the finish line. Unfortunately, the watch reports only seconds: You know that your race took 9315 seconds, but you would like to know your time in hh:mm:ss form. How would you calculate it?
Then return to Java, and write the same procedure, now extended to include days as well as hh:mm:ss. Once you understand how the calculation is made, the rest is just a matter of expressing it in Java.
 Signature Eric.Sosman@sun.com
Dr J R Stockton - 23 May 2007 19:02 GMT In comp.lang.java.programmer message <4652ee26$1_4@news.bluewin.ch>, Tue, 22 May 2007 15:20:25, Alexandre Jaquet <alexjaquet@gmail.com> posted:
> long time = 1000*3600*24; //A day in milliseconds That is 24 hours. Ignoring Leap Seconds may well be safe.
Remember that in many locations, one day each year has 23 hours and one day has 25 hours; 23.5 and 24.5 also occur.
 Signature (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6. Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Z. - 25 May 2007 03:42 GMT > Hi, > I'm looking for a way to retrieve the day, hour and sec between two > dates object Wouldn't
Calendar deltaTime = date1 - date2;
Work?
Lew - 25 May 2007 12:32 GMT >> Hi, >> I'm looking for a way to retrieve the day, hour and sec between two [quoted text clipped - 5 lines] > > Work? No.
Let's assume 'date1' and 'date2' in your example are of type long. Then you'd use:
Calendar deltaTime = Calendar.getInstance(); deltaTime.setTimeInMillis( date1 - date2 );
Then you'd parse out the Calendar for day, hour, sec, ...
 Signature Lew
Z@nospam.com - 27 May 2007 19:47 GMT > >> I'm looking for a way to retrieve the day, hour and sec between two > >> dates object
> > Wouldn't > > Calendar deltaTime = date1 - date2; > > Work?
> No. > > Let's assume 'date1' and 'date2' in your example are of type long. Why assume that when the OP said they were "date objects." And by "date objects," I assume he means Calendar objects.
Lew - 28 May 2007 00:13 GMT >>>> I'm looking for a way to retrieve the day, hour and sec between two >>>> dates object [quoted text clipped - 9 lines] > Why assume that when the OP said they were "date objects." And by > "date objects," I assume he means Calendar objects. Because otherwise the use of the subtraction operator makes no sense. You can't "subtract" Calendar objects.
There are at least three kinds of "date" in Java, java.util.Date (and its subclass, java.sql.Date), java.util.Calendar, and long.
 Signature Lew
Lew - 28 May 2007 00:18 GMT >>>>> I'm looking for a way to retrieve the day, hour and sec between two >>>>> dates object [quoted text clipped - 15 lines] > There are at least three kinds of "date" in Java, java.util.Date (and > its subclass, java.sql.Date), java.util.Calendar, and long. If you don't want to assume date1 and date2 are longs, then you make a lemma: let d1 and d2 be the original non-long (Date or Calendar?) objects extract the long equivalents (e.g., through Date.getTime()) into variables date1 and date2, declared as type long.
Go back to where I said "assume date1 and date2 are longs" and resume the instructions.
 Signature Lew
Z@nospam.com - 28 May 2007 01:50 GMT >> Why assume that when the OP said they were "date objects." And by >> "date objects," I assume he means Calendar objects.
> Because otherwise the use of the subtraction operator makes no sense. > You can't "subtract" Calendar objects. Ahh, Ok, well that was my question. In C# I *can* subtract date objects to get delta time, the + and - operators are overloaded.
I assumed Java's classes also overloaded + and -. Now I know.
Lew - 28 May 2007 01:57 GMT Lew wrote:
>> You can't "subtract" Calendar objects.
> Ahh, Ok, well that was my question. In C# I *can* subtract date > objects to get delta time, the + and - operators are overloaded. > > I assumed Java's classes also overloaded + and -. Now I know. Only String, which overloads '+'. The Designers made a conscious choice not to support operator overloading.
 Signature Lew
Roedy Green - 28 May 2007 11:02 GMT On Tue, 22 May 2007 15:20:25 +0200, Alexandre Jaquet <alexjaquet@gmail.com> wrote, quoted or indirectly quoted someone who said :
>I'm looking for a way to retrieve the day, hour and sec between two >dates object [quoted text clipped - 5 lines] > long day = daterange/time; > auctionDurationRest = " jour : " + day; see http://mindprod.com/jgloss/time.html#MIXEDBASE --
Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|