-1

enter image description here

^this what I am trying to emulate.

And yes, I am recently trying to learn Java and I feel lost in this exercise.

I have done some searching to find pieces of code to use, I am also using the textbook: Big Java Late Objects.

I'm looking to write a Java application where the user enters the time of a ski runner's finish time (for a race called Vasaloppet). The start time is assumed to be at 08:00 for all participants, this is hard-coded.

The app will display the time it took for the skier to get from Salen to Mora and then present this in hours, minutes and seconds.

The fastest time anyone has finished Vasaloppet is in 2012 when Jörgen Brink crossed the finish line with the time 03:38:41. The program will now present how much slower (or faster), today's skiers were.

I want to convert time to seconds, take the time (in seconds) minus the record time (in seconds). The result is then converted back to hours, minutes and seconds to be presented.

This is my code so far:

import java.util.Scanner;   /// makes the scanner class available

public class Vasalopp {

public static void main(String[] args) {


    System.out.println("Vasaloppet - Input your finish time");  /// headline
    System.out.println("------------------------------------------------------------------");   /// for stylish purpose

    Scanner input = new Scanner(System.in); /// scanner object to make the application able to read keyboard input
    System.out.print("Hours: ");
    String inputHours = input.nextLine(); ///user promted to input number of hours
    System.out.print("Minutes: "); 
    String inputMinutes = input.nextLine(); ///user promted to input number of minutes
    System.out.print("Seconds: "); 
    String inputSeconds = input.nextLine(); ///user promted to input number of seconds

    /// constants
    int SECONDS_IN_AN_HOUR = 3600;
    int MINUTES_IN_AN_HOUR = 60;
    int SECONDS_IN_A_MINUTE = 60;
    int STARTING_TIME = 28800;
    int RECORD_TIME = 13121;


    System.out.print("Your time is: \t");
    System.out.println(); 
    System.out.println("------------------------------------------------------------------"); 

    System.out.print("Record time 2012 were: \t"); 
    System.out.println();
    System.out.println("------------------------------------------------------------------"); 

    System.out.print("The time differences is: \t");
    System.out.println("------------------------------------------------------------------"); 


}
}

I basically have 2 issues that I can't solve:

  1. how do I make it possible for a user to input a finish time, in this case 14:10:55?
  2. Can anyone give me a hint on how to solve the time conversion?:

I know the math has to go like this for example:

Record time is 03:38:41, which can be written as

  • 13121 seconds 13121/3600 = 3.644722222 <-- 3 hours
  • (3.644722222-3)*60 = 38.68333333 <-- 38 minutes
  • (38.68333333-38)*60 = 41 seconds.

I'm aware of modulus %, but not sure how to use it properly

Basically, I can't figure out how to interpret this into Java code. I tried a few methods, but didn't seem to get it right.

Thanks a lot.

wiro
  • 138
  • 2
  • 17

2 Answers2

1

If you can, I recommend Joda, e.g.

Period p = new Period(RECORD_TIME * 1000L);
System.out.println(p.getHours());
System.out.println(p.getMinutes());
System.out.println(p.getSeconds())

Otherwise, you can do the calculation manually

int hours = RECORD_TIME / SECONDS_IN_AN_HOUR;
int minutes = (RECORD_TIME - SECONDS_IN_AN_HOUR * hours) / MINUTES_IN_AN_HOUR;
int seconds = RECORD_TIME % SECONDS_IN_A_MINUTE;
radoh
  • 4,554
  • 5
  • 30
  • 45
-1

You can get Date from String though SimpleDateFormat

    String time = "14:10:55";
    SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
    Date date = formatter.parse(time);

use Calendar to allocate hours, minutes and seconds

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    long hour = c.get(Calendar.HOUR_OF_DAY);
    long minutes = c.get(Calendar.MINUTE);
    long seconds = c.get(Calendar.SECOND);

and for calculating time delta use milliseconds

    long delta = date.getTime() - bestIn2012.getTime();
    Calendar cc = Calendar.getInstance();
    cc.setTimeInMillis(delta);