TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-15-2008, 01:08 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Calculate

I have a script, actually in Java, but nevermind that. And it's supposed to let the user print in starttime in hours, minutes and seconds. And then the endtime in hours, minutes and seconds.

Then it calculates how many hours, minutes, and seconds it took.
Obviously it's a bit trickier than just endseconds - startseconds etc, since the startseconds could be more than the endseconds, which would result in that it took 1 more minute.

I think I recall that I had to use "%" but not sure how.
Please help. If it's to any help, here's the javacode if someone knows java, but it's really just the math calculation I need help with.

java Code:
import java.util.*;

public class Calculate
{
    public static void main(String[] args)
    {
       
        // Deklarera variabler
        String start, end, nextStart, nextEnd;
        int start_hours, start_minutes, start_seconds, end_hours, end_minutes, end_seconds, result_hours, result_minutes, result_seconds;
       
        // Initialisera den importerade klassen
        Scanner keyboard = new Scanner(System.in);
       
        // Start
        System.out.print("Starttid (tt:mm:ss): ");
        start = keyboard.next();
       
        // Mål
        System.out.print("Målgång (tt:mm:ss): ");
        end = keyboard.next();
       
        // Rensa
        keyboard.nextLine();
       
        StringTokenizer startTime = new StringTokenizer(start, ":");
        StringTokenizer endTime = new StringTokenizer(end, ":");
       
        nextStart = startTime.nextToken();
        start_hours = Integer.parseInt(nextStart);
        nextStart = startTime.nextToken();
        start_minutes = Integer.parseInt(nextStart);
        nextStart = startTime.nextToken();
        start_seconds = Integer.parseInt(nextStart);
       
        nextEnd = endTime.nextToken();
        end_hours = Integer.parseInt(nextEnd);
        nextEnd = endTime.nextToken();
        end_minutes = Integer.parseInt(nextEnd);
        nextEnd = endTime.nextToken();
        end_seconds = Integer.parseInt(nextEnd);
       
        System.out.println("\nStarttid:\nTimme: " + start_hours + "\nMinut: " + start_minutes + "\nSekund: " + start_seconds);
        System.out.println("\nMålgångstid:\nTimme: " + end_hours + "\nMinut: " + end_minutes + "\nSekund: " + end_seconds);
        System.out.println("\nResultat: Blaa");
       
    }
}
__________________
Tanax is offline  
Reply With Quote
Old 10-22-2008, 11:03 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

I am barely starting with java myself, but maybe this links would help javaworld and java2s.
although they also use dates, they also use time in hours and minutes and such.
tony is offline  
Reply With Quote
The Following User Says Thank You to tony For This Useful Post:
Tanax (10-23-2008)
Old 10-23-2008, 10:53 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks! Didn't really solve my code any better though, but I learned alot from reading.. so thanks!
__________________
Tanax is offline  
Reply With Quote
Old 10-24-2008, 12:46 AM   #4 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

this might help
Java Time Calculator - Java - Programming
SourceForge.net: Java Time Calculator

not sure :P
codefreek is offline  
Reply With Quote
The Following User Says Thank You to codefreek For This Useful Post:
Tanax (10-24-2008)
Old 10-24-2008, 10:11 AM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ye, they solved it the same way I did basicly. Calculate to seconds, then devide it to get hours, extract those seconds. Devide the new number of seconds to get minutes, extract those seconds. Then you get the rest of the seconds..

Thanks!
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 03:33 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design