Android Java:如何减去两次?

发布于 2024-09-14 18:24:54 字数 209 浏览 1 评论 0原文

我在我的项目中使用某种秒表,我需要

start time ex: 18:40:10 h
stop time  ex: 19:05:15 h

这两个值的结果,例如final time = stop - start

我找到了一些示例,但它们都非常令人困惑。

有什么简单的解决办法吗?

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

I found some examples but they all are very confusing .

Is there any simple solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鱼忆七猫命九 2024-09-21 18:24:54

如果您有字符串,则需要使用 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();

这里 diff 是 18:40:10 和 19:05:15 之间经过的毫秒数。

编辑1:

在网上找到了一个方法(位于 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;

编辑2:

如果你想要它作为一个字符串(这是一种草率的方式,但它有效):

String diffTime = (hours<10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds) + " h";

编辑3:

如果你想要毫秒,就这样做

long timeMS = diff % 1000;

你可以将其除以1000以获得秒的小数部分。

If you have strings you need to parse them into a java.util.Date using java.text.SimpleDateFormat. Something like:

        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.

EDIT 1:

Found a method online for this (at 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:

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:

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.

滥情稳全场 2024-09-21 18:24:54

假设您正在使用 java.util.Date:

long totalTime = endDate.getTime() - startDate.getTime();

结果将以毫秒为单位的总时间。

Assuming you are using java.util.Date:

long totalTime = endDate.getTime() - startDate.getTime();

The result will be the total time in milliseconds.

空袭的梦i 2024-09-21 18:24:54

我正在提供现代答案。

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

此示例的输出为:

0小时25分5秒

其他答案在 2010 年都是很好的答案。今天避免使用 DateFormatSimpleDateFormatDate 类。 java.time,现代 Java 日期和时间 API,使用起来要好得多。

但它不需要 API 级别 26 吗?

不,使用 java.time 在旧版和新版 Android 设备上都能很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及较新的 Android 设备(从 API 级别 26 开始)中,现代 API 是内置的。
  • 在非 Android Java 6 和 7 中,获取 ThreeTen Backport,即现代类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保使用子包从 org. Threeten.bp 导入日期和时间类。

链接

I am providing the modern answer.

java.time and 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

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.

But doesn’t it require API level 26?

No, using java.time works nicely on older and newer Android devices. It just requires at least 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.

Links

客…行舟 2024-09-21 18:24:54

尝试这个答案,它会对您有很大帮助,

此处

在这个答案中,您会发现减去两次丢弃日期和月份和年份。

它给出了达到第二个时间值之前将花费的分钟数。

Try this answer it will help you a lot

from here

In this answer, you will find subtract to two times discarding the day and month and year.

it gives you the number of minutes you will spend until you reach the second Time value.

桃扇骨 2024-09-21 18:24:54

如今,有了较新的 Java(不知道在什么 Android 版本上可以使用):

Instant before = Instant.now();
// do stuff

Duration.between(before, Instant.now()).getSeconds()

过去笨拙的 Java 方式现在已经不复存在了。

Today, with newer Java (no idea, at what Android version this works):

Instant before = Instant.now();
// do stuff

Duration.between(before, Instant.now()).getSeconds()

The clumsy java ways from olden days are now gone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文