For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2004 > Calculating time









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Calculating time
Ben

2004-03-27, 12:30 am

I'm stumped :-(

I can't create an algorithm for this ticklish problem, nor think of any
pseudocode; yet i know exactly what to do in my head. I can't even work it
out on a calculator, let alone find a suitable solution i could code into a
java class.

The difference in time between 15:37 and 14:59 is 23 hours and 38 minutes.

Any feedback or web references would be greatfully received.

Thanks in advance,
Ben.


Roedy Green

2004-03-27, 12:30 am

On Thu, 25 Mar 2004 01:10:38 +0800, "Ben" <dontwant@togetspam.com>
wrote or quoted :

>The difference in time between 15:37 and 14:59 is 23 hours and 38 minutes.


see http://mindprod.com/jgloss/calendar.html

You just subtract two timestamps to get the difference in
milliseconds.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Ben

2004-03-27, 12:30 am

Hi Roedy,

I'm want to be able to supply the time manually in military time. I just
can't seem to break the problem down in order to be able to solve it.
Here's what I attempted, but to be honest, i really don't know what I'm
doing. :-(

GregorianCalendar timeOne = new GregorianCalendar();

GregorianCalendar timeTwo = new GregorianCalendar();


timeOne.set(2004,3,25,15,37,0);

timeTwo.set(2004,3,25,14,59,0);


double hours = ((double)(timeOne.getTimeInMillis() -
timeTwo.getTimeInMillis()) / (60*60*1000));


System.out.println(hours);

Thanks Roedy,
Ben.


"Roedy Green" <look-at-the-website@mindprod.com> wrote in message
news:15n3609mpg997hpkql68kvrvf9mg6kh7gn@
4ax.com...
> On Thu, 25 Mar 2004 01:10:38 +0800, "Ben" <dontwant@togetspam.com>
> wrote or quoted :
>
minutes.[color=darkred]
>
> see http://mindprod.com/jgloss/calendar.html
>
> You just subtract two timestamps to get the difference in
> milliseconds.
>
> --
> Canadian Mind Products, Roedy Green.
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.



GV

2004-03-27, 12:30 am

Your logic in your original post is not quite right. It's either 38 minutes
or it's 23 hrs, 22 minutes. Your second post was close. Maybe this helps a
little:

long millisPerMinute = 1000 * 60;
long millisPerHour = millisPerMinute * 60;
// 3/23/04 14:59
GregorianCalendar day2 = new GregorianCalendar(2004, 2, 23, 14, 59);
// 3/22/04 15:37
GregorianCalendar day1 = new GregorianCalendar(2004, 2, 22, 15, 37);

long time2 = day2.getTimeInMillis();
long time1 = day1.getTimeInMillis();
long timeDiff = time2 - time1;
long hours = timeDiff/millisPerHour;
long minutes = (timeDiff%millisPerHour)/millisPerMinute;
StringBuffer buf = new StringBuffer("Elapsed time: ");
if (hours > 0)
buf.append(hours).append(" hrs");
if (minutes > 0)
buf.append(", ").append(minutes).append(" mins");
String msg = buf.toString();
System.out.println(msg);

Output: Elapsed time: 23 hrs, 22 mins.

-GV

"Ben" <dontwant@togetspam.com> wrote in message
news:40625e24$1@funnel.arach.net.au...

> Hi Roedy,
>
> I'm want to be able to supply the time manually in military time. I just
> can't seem to break the problem down in order to be able to solve it.
> Here's what I attempted, but to be honest, i really don't know what I'm
> doing. :-(
>
> GregorianCalendar timeOne = new GregorianCalendar();
>
> GregorianCalendar timeTwo = new GregorianCalendar();
>
>
> timeOne.set(2004,3,25,15,37,0);
>
> timeTwo.set(2004,3,25,14,59,0);
>
>
> double hours = ((double)(timeOne.getTimeInMillis() -
> timeTwo.getTimeInMillis()) / (60*60*1000));
>
>
> System.out.println(hours);
>
> Thanks Roedy,
> Ben.
>
>
> "Roedy Green" <look-at-the-website@mindprod.com> wrote in message
> news:15n3609mpg997hpkql68kvrvf9mg6kh7gn@
4ax.com...
> minutes.
>
>



Ben

2004-03-27, 12:30 am

Thanks GV. That really helped heaps. Hope to return the favour one day :-)

Regards,
Ben.

"GV" <nobody@home.com> wrote in message
news:10654n8e9cqk039@corp.supernews.com...
> Your logic in your original post is not quite right. It's either 38

minutes
> or it's 23 hrs, 22 minutes. Your second post was close. Maybe this helps

a
> little:
>
> long millisPerMinute = 1000 * 60;
> long millisPerHour = millisPerMinute * 60;
> // 3/23/04 14:59
> GregorianCalendar day2 = new GregorianCalendar(2004, 2, 23, 14, 59);
> // 3/22/04 15:37
> GregorianCalendar day1 = new GregorianCalendar(2004, 2, 22, 15, 37);
>
> long time2 = day2.getTimeInMillis();
> long time1 = day1.getTimeInMillis();
> long timeDiff = time2 - time1;
> long hours = timeDiff/millisPerHour;
> long minutes = (timeDiff%millisPerHour)/millisPerMinute;
> StringBuffer buf = new StringBuffer("Elapsed time: ");
> if (hours > 0)
> buf.append(hours).append(" hrs");
> if (minutes > 0)
> buf.append(", ").append(minutes).append(" mins");
> String msg = buf.toString();
> System.out.println(msg);
>
> Output: Elapsed time: 23 hrs, 22 mins.
>
> -GV
>
> "Ben" <dontwant@togetspam.com> wrote in message
> news:40625e24$1@funnel.arach.net.au...
>
just[color=darkred]
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com