Android Java:如何减去两次?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3514639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Java : How to subtract two times?
提问by Nezir
I use some kind of stopwatch in my project and I have
我在我的项目中使用了某种秒表,我有
start time ex: 18:40:10 h
stop time ex: 19:05:15 h
I need a result from those two values like final time = stop - start
我需要这两个值的结果,例如 final time = stop - start
I found some examples but they all are very confusing .
我找到了一些例子,但它们都非常令人困惑。
Is there any simple solution ?
有什么简单的解决办法吗?
采纳答案by John
If you have strings you need to parse them into a java.util.Date using java.text.SimpleDateFormat. Something like:
如果您有字符串,则需要使用 java.text.SimpleDateFormat 将它们解析为 java.util.Date。就像是:
java.text.DateFormat df = new java.text.SimpleDateFormat("hh:mm:ss");
java.util.Date date1 = df.parse("18:40:10");
java.util.Date date2 = df.parse("19:05:15");
long diff = date2.getTime() - date1.getTime();
Here diff is the number of milliseconds elapsed between 18:40:10 and 19:05:15.
这里的 diff 是 18:40:10 和 19:05:15 之间经过的毫秒数。
EDIT 1:
编辑 1:
Found a method online for this (at http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html?page=2):
在网上找到了一个方法(在http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html?page=2):
int timeInSeconds = diff / 1000;
int hours, minutes, seconds;
hours = timeInSeconds / 3600;
timeInSeconds = timeInSeconds - (hours * 3600);
minutes = timeInSeconds / 60;
timeInSeconds = timeInSeconds - (minutes * 60);
seconds = timeInSeconds;
EDIT 2:
编辑2:
If you want it as a string (this is a sloppy way, but it works):
如果你想要它作为一个字符串(这是一种草率的方式,但它有效):
String diffTime = (hours<10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds) + " h";
EDIT 3:
编辑 3:
If you want the milliseconds just do this
如果你想要毫秒就这样做
long timeMS = diff % 1000;
You can then divide that by 1000 to get the fractional part of your seconds.
然后,您可以将其除以 1000 以获得秒的小数部分。
回答by Thierry-Dimitri Roy
Assuming you are using java.util.Date:
假设您使用的是java.util.Date:
long totalTime = endDate.getTime() - startDate.getTime();
The result will be the total time in milliseconds.
结果将以毫秒为单位的总时间。
回答by Momen Ali
回答by Ole V.V.
I am providing the modern answer.
我正在提供现代答案。
java.time and ThreeTenABP
java.time 和 ThreeTenABP
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("H:mm:ss 'h'");
String startTimeString = "18:40:10 h";
String stopTimeString = "19:05:15 h";
LocalTime startTime = LocalTime.parse(startTimeString, timeFormatter);
LocalTime stopTime = LocalTime.parse(stopTimeString, timeFormatter);
if (stopTime.isBefore(startTime)) {
System.out.println("Stop time must not be before start time");
} else {
Duration difference = Duration.between(startTime, stopTime);
long hours = difference.toHours();
difference = difference.minusHours(hours);
long minutes = difference.toMinutes();
difference = difference.minusMinutes(minutes);
long seconds = difference.getSeconds();
System.out.format("%d hours %d minutes %d seconds%n", hours, minutes, seconds);
}
Output from this example is:
这个例子的输出是:
0 hours 25 minutes 5 seconds
0 小时 25 分 5 秒
The other answers were good answers in 2010. Today avoid the classes DateFormat
, SimpleDateFormat
and Date
. java.time, the modern Java date and time API, is so much nicer to work with.
其他答案是 2010 年的好答案。今天避免上课DateFormat
,SimpleDateFormat
和Date
。java.time 是现代 Java 日期和时间 API,使用起来要好得多。
But doesn't it require API level 26?
但是它不需要 API 级别 26 吗?
No, using java.time works nicely on older and newer Android devices. It just requires at least Java 6.
不,在较旧和较新的 Android 设备上使用 java.time 效果很好。它只需要至少Java 6。
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
- 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。
- 在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
- 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从
org.threeten.bp
子包中导入日期和时间类。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Oracle 教程:解释如何使用 java.time 的日期时间。
- Java 规范请求 (JSR) 310,
java.time
首先描述的地方。 - ThreeTen Backport 项目,
java.time
向 Java 6 和 7 (ThreeTen for JSR-310)的向后移植。 - ThreeTenABP, ThreeTen Backport安卓版
- 问题:如何在Android项目中使用ThreeTenABP,讲解很透彻。
回答by Frischling
Today, with newer Java (no idea, at what Android version this works):
今天,使用更新的 Java(不知道,它在哪个 Android 版本上有效):
Instant before = Instant.now();
// do stuff
Duration.between(before, Instant.now()).getSeconds()
The clumsy java ways from olden days are now gone.
过去笨拙的 Java 方式现在已经一去不复返了。