0

该程序将向用户显示程序的开始时间、结束时间和经过时间。例如,如果程序在 09:23:45 开始并在 09:23:55 结束,那么对用户的输出将是这样的,开始时间:09h:23m:45s 结束时间:09h:23m:55s 经过时间 00h :00 米:10 秒。我在显示时间时遇到问题...请帮助

这是主要的

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());



}
}

主要错误

计时班

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));
}




 }

TimeWatch 类的错误

4

1 回答 1

1

根据oracle 文档, System.nanoTime() 与时钟无关。所以,在这种情况下,我们大概可以使用System.currentTimeMillis();来启动定时器。一旦计时器停止,我们可以使用以下代码打印间隔:

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);
}
于 2016-02-28T01:08:56.993 回答