Java 如何在android中添加两毫秒

Java 如何在android中添加两毫秒,java,android,eclipse,mobile,Java,Android,Eclipse,Mobile,我想计算两次之间的差,这是正确的计算,然后我必须把它减半,所以我用2除以它,结果是可以的。但是,当我试图添加Timdedifferencemills到startTime时,它并没有给我正确的结果 starttime=05:53 结束时间=17:57 我想要11:55的结果 但是我的代码给了我06:55 请帮忙 protected String transitTime2(String endtime, String starttime) { SimpleDateFormat dt = ne

我想计算两次之间的差,这是正确的计算,然后我必须把它减半,所以我用2除以它,结果是可以的。但是,当我试图添加Timdedifferencemills到startTime时,它并没有给我正确的结果

starttime=05:53
结束时间=17:57
我想要11:55的结果
但是我的代码给了我06:55 请帮忙

protected String transitTime2(String endtime, String starttime) {
    SimpleDateFormat dt = new SimpleDateFormat("hh:mm");
    Date startTime = null;
    Date endTime;
    long timdedifferencemillis = 0;
    try {
        startTime = dt.parse(starttime);
        endTime = dt.parse(endtime);
        long diff=startTime.getTime();
        timdedifferencemillis = (endTime.getTime() - startTime.getTime())/2;
        //timdedifferencemillis

    } catch (ParseException e) {
        e.printStackTrace();
    }
    long timdedifferencemillis1=startTime.getTime()+timdedifferencemillis;

    int minutes = Math
            .abs((int) ((timdedifferencemillis1 / (1000 * 60)) % 60));
    int hours = Math
            .abs((int) ((timdedifferencemillis1 / (1000 * 60 * 60)) % 24));
    String hmp = String.format("%02d %02d ", hours, minutes);
    return hmp;
}

问题可能是时区;默认情况下,当您最初解析
endtime
starttime
时(在没有在格式字符串中指明并在输入中表示的明确时区的情况下),Java假定提供的时间是相对于系统的本地时区的。然后,当你打电话时,它会回来

自1970年1月1日00:00:00GMT以来的毫秒数,由该
Date
对象表示

一种解决方案是告诉您的
SimpleDataFormat
对象假定它解析的所有字符串都是GMT,而不是本地时区。在初始化
dt
之后,但在调用
dt.parse(…)
之前,尝试添加此行:


我认为问题在于代码中的“long”和“int”类型;当我们用2(long-timdedifferencemills)除时,结果必须是“double”。

使用java 8中新的
java.time
API或JODA时间库很容易做到这一点:

import java.time.Duration;
import java.time.LocalTime;

public class TimeDiff {
    public static void main(String[] args) {
        LocalTime start = LocalTime.parse("05:53");
        LocalTime end = LocalTime.parse("17:57");
        // find the duration between the start and end times
        Duration duration = Duration.between(start, end);
        // add half the duration to the start time to find the midpoint
        LocalTime midPoint = start.plus(duration.dividedBy(2));
        System.out.println(midPoint);
    }
}
输出:

11:55


通过使用
LocalTime
对象,可以避免时区出现任何问题。

17:57-05:53=12:04。为什么你期望11:55,为什么你要除以2?哦,你想在两者之间找到时间的一半?@DavidConrad是的,你是对的12.04是差,它的一半是06:02。现在我想在startTime中添加06:02。所以05:53+06:02=11:55。这就是我试图做的,如果你能提出一些建议…有时区问题,我从下面的答案中得到了这个答案,现在它工作准确。。。。
import java.time.Duration;
import java.time.LocalTime;

public class TimeDiff {
    public static void main(String[] args) {
        LocalTime start = LocalTime.parse("05:53");
        LocalTime end = LocalTime.parse("17:57");
        // find the duration between the start and end times
        Duration duration = Duration.between(start, end);
        // add half the duration to the start time to find the midpoint
        LocalTime midPoint = start.plus(duration.dividedBy(2));
        System.out.println(midPoint);
    }
}