0

This program will display the start time, end time and elapse time of a program to the user. For example if the program started at 09:23:45 and ended at 09:23:55 then the output to the user would be as such, start time: 09h:23m:45s end time: 09h:23m:55s elapsed time 00h:00m:10s. I am having issues will displaying the time... Please help

this is the main

import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;
public class ElapsedTimeWatch {

public static void main(String... args) throws InterruptedException {

    TimeWatch watch = TimeWatch.start();
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH'h':mm'm':ss's'");
    System.out.println("Start Time is : " + dateFormat.format(timestart));
     String.format("%02dh:%02dm:%02ds",
                    TimeUnit.MILLISECONDS.toHours(watch.timestart()),//dateFormat.format(start)
                    TimeUnit.MILLISECONDS.toSeconds(watch.timestart()) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(watch.timestart())));









    Thread.sleep(1000 * 10);



    System.out.println("End Time is : " + dateFormat.format(timeend));
     String.format("%02dh:%02dm:%02ds",
                    TimeUnit.MILLISECONDS.toHours(watch.timeend()),
                    TimeUnit.MILLISECONDS.toSeconds(watch.timeend()) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(watch.timeend())));









    System.out.println("Elapsed Time custom format: " + watch.toMinuteSeconds());



}
}

Errors for Main

Time Watch Class

import java.util.concurrent.TimeUnit;

public class TimeWatch {

long starts;



private TimeWatch() {
    reset();
}

public static TimeWatch start() {
    return new TimeWatch();
}

public TimeWatch reset() {
    starts = System.currentTimeMillis();
    return this;
}




 public long time() {
    long ends = System.currentTimeMillis();
    return ends - starts;
}

//Start Time
public long timestart() {
   starts = System.currentTimeMillis();
    return starts;
}
//End Time
public long timeend() {
    long ends = System.currentTimeMillis();
    return ends;
}





public long time(TimeUnit unit) {
    return unit.convert(time(), TimeUnit.MILLISECONDS);


public String toMinuteSeconds(){
    return String.format("%d min, %d sec", time(TimeUnit.MINUTES),
            time(TimeUnit.SECONDS) - time(TimeUnit.MINUTES));
}




 }

Errors for TimeWatch Class

mar
  • 43
  • 1
  • 6
  • *"I am having issues will displaying the time"* What issue? Be specific. What are you getting and what were you expecting? – Andreas Feb 28 '16 at 00:45
  • Added screenshort appears to show what you are getting. I see nothing wrong with that. As I asked in previous comment, **what were you expecting?** – Andreas Feb 28 '16 at 02:48
  • Sorry for the miscommunication - For example if the program started at 09:23:45 and ended at 09:23:55 the output to the user would be as such, start time: 09h:23m:45s end time: 09h:23m:55s elapsed time 00h:00m:10s my program does not format like above? – mar Feb 28 '16 at 02:56
  • Of course not. Which part of your code did you expect to format like that? You explicitly coded to format as `Start Time is : %d min, %d sec`. For your desired output, you'd need `Start Time is : %02dh:%02dm:%02ds` – Andreas Feb 28 '16 at 03:02
  • _Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%02d' at java.util.Formatter.format(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.lang.String.format(Unknown Source) at ElapsedTimeWatch.main(ElapsedTimeWatch.java:10)_ Theses errors I get when I just change this %d min, %d sec to %02dh:%02dm:%02ds?? – mar Feb 28 '16 at 05:03
  • Yes, because hours + minutes + seconds is **3** values, not 2. – Andreas Feb 28 '16 at 08:50

1 Answers1

1

As per the oracle documentation, System.nanoTime() is not related to clock. So, in this case, we can probably use System.currentTimeMillis(); to start the timer. Once the timer is stopped, we can use the following code to print the lapse:

public static void main(String[] args) throws InterruptedException {
    long start = System.currentTimeMillis();
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH'h':mm'm':ss's'");
    Thread.sleep(10000);
    long end = System.currentTimeMillis();
    long difference = end - start;
    System.out.println("Start :" + dateFormat.format(start) );
    System.out.println("End :" + dateFormat.format(end) );
    String format = String.format("%d min, %d sec", 
            TimeUnit.MILLISECONDS.toMinutes(difference),
            TimeUnit.MILLISECONDS.toSeconds(difference) - 
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(difference))
        );
    System.out.println(format);
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Thank You! However, I am kinda new to Programming.... How would I go about implementing the dateFormat? Can I add it directly into the main or do I have to use a method to call for it from the class? – mar Feb 28 '16 at 03:01
  • @mar Look at the code. Line 3 creates `dateFormat`. You don't have to implement it. – Andreas Feb 28 '16 at 03:06
  • You just need to import SimpleDateFormat class into your class (e.g. `import java.text.SimpleDateFormat;`). – Darshan Mehta Feb 28 '16 at 03:08