如何测量Java中经过的时间? [重复]
本文翻译自:How do I measure time elapsed in Java? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- How do I time a method's execution in Java? 如何在Java中计时方法的执行时间? 38 answers 38个答案
- How do I calculate the elapsed time of an event in java? 如何计算Java中事件的经过时间? [duplicate] 6 answers [重复] 6个答案
I want to have something like this: 我想要这样的东西:
public class Stream
{public startTime;public endTime;public getDuration(){return startTime - endTime;}
}
Also it is important that for example if the startTime it's 23:00 and endTime 1:00 to get a duration of 2:00. 同样重要的是,例如,如果startTime为23:00,endTime为1:00,则持续时间为2:00。
Which types to use in order to accomplish this in Java? 为了在Java中完成此操作,应使用哪些类型?
#1楼
参考:https://stackoom.com/question/7QSY/如何测量Java中经过的时间-重复
#2楼
如果要从System.currentTimeMillis()获取时间戳,则您的时间变量应为long。
#3楼
Java provides the static method System.currentTimeMillis() . Java提供了静态方法System.currentTimeMillis() 。 And that's returning a long value, so it's a good reference. 而且返回的是一个长整数,因此它是一个很好的参考。 A lot of other classes accept a 'timeInMillis' parameter which is long as well. 许多其他类也接受一个'timeInMillis'参数,该参数也很长。
And a lot of people find it easier to use the Joda Time library to do calculations on dates and times. 许多人发现使用Joda Time库更容易地进行日期和时间计算。
#4楼
Your new class: 您的新课程:
public class TimeWatch { long starts;public static TimeWatch start() {return new TimeWatch();}private TimeWatch() {reset();}public TimeWatch reset() {starts = System.currentTimeMillis();return this;}public long time() {long ends = System.currentTimeMillis();return ends - starts;}public long time(TimeUnit unit) {return unit.convert(time(), TimeUnit.MILLISECONDS);}
}
Usage: 用法:
TimeWatch watch = TimeWatch.start();// do somethinglong passedTimeInMs = watch.time();long passedTimeInSeconds = watch.time(TimeUnit.SECONDS);
Afterwards, the time passed can be converted to whatever format you like, with a calender for example 之后,可以将经过的时间转换为所需的任何格式,例如使用压光机
Greetz, GHad 加的特Greetz
#5楼
If you prefer using Java's Calendar API you can try this, 如果您更喜欢使用Java的Calendar API ,可以尝试一下,
Date startingTime = Calendar.getInstance().getTime();
//later on
Date now = Calendar.getInstance().getTime();
long timeElapsed = now.getTime() - startingTime.getTime();
#6楼
Which types to use in order to accomplish this in Java? 为了在Java中完成此操作,应使用哪些类型?
Answer : long 答 :长
public class Stream {public long startTime;public long endTime;public long getDuration() {return endTime - startTime;}// I would addpublic void start() {startTime = System.currentTimeMillis();}public void stop() {endTime = System.currentTimeMillis();}
}
Usage: 用法:
Stream s = .... s.start();// do something for a while s.stop();s.getDuration(); // gives the elapsed time in milliseconds.
That's my direct answer for your first question. 这是我第一个问题的直接答案。
For the last "note" I would suggest you to use Joda Time. 对于最后一个“笔记”,我建议您使用Joda Time。 It contains an interval class suitable for what you need. 它包含一个适合您需要的时间间隔类。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
