如何在Java中计算时差?

如何在Java中计算时差?,java,Java,有没有办法计算两次之间的准确时差 因为。e、 g t1= 11:18 t2= 20:20 t2 - t1 = 9.02 我已经尝试过这个代码,但它没有帮助 String hour1=h1+":"+m1; String hour2= h2+":"+m2; SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm"); Date d1= dateFormat.parse(hour1); Date d2= dateFormat.parse(ho

有没有办法计算两次之间的准确时差

因为。e、 g

t1= 11:18 
t2= 20:20
t2 - t1 = 9.02
我已经尝试过这个代码,但它没有帮助

String hour1=h1+":"+m1;
String hour2= h2+":"+m2;
SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm");
Date d1= dateFormat.parse(hour1);
Date d2= dateFormat.parse(hour2);
long d=d2.getTime()-d1.getTime();
long hour= d/(60*60*1000) % 24;

通过使用此代码,我无法找到分钟差。

我使用以下代码转换到不同的时区并检查时间差。你可以抓取以毫秒为单位的时间,然后像我在下面为你的案例所做的那样比较这些值。只要把timeInMillisOne减去timeInMillisTwo

Date date = dateFormat.parse("");
Calendar cal = Calendar.getInstance(); // that is NOW for the timezone configured on the computer.

long timeInMillis = cal.getTimeInMillis(); 
cal.setTimeInMillis(timeInMillis + (60000 * 60 * 5) - (60000 * 15));
//the line above converts to a different time zone and subtracts fifteen 
//minutes. 
long fifteenMinutesAgo = cal.getTime();

if(date.after(fifteenMinutesAgo)){
    //do something
}
这可能行得通

package cn.sz.cyrus.java8test;

public class TimeCalcute {

    public static String calcTime(String time1, String time2) {
        String[] times1 = time1.split(":");
        String[] times2 = time2.split(":");
        int timeint1 = Integer.valueOf(times1[0]) * 60 + Integer.valueOf(times1[1]);
        int timeint2 = Integer.valueOf(times2[0]) * 60 + Integer.valueOf(times2[1]);
        int interval = timeint1 - timeint2;
        String result = String.format("%d:%02d", interval / 60, interval % 60);
        return result;
    }

    public static void main(String[] args) {
        String time1 = "20:20";
        String time2 = "11:18";
        System.out.println(calcTime(time1, time2));
        time1 = "22:46";
        time2 = "10:59";
        System.out.println(calcTime(time1, time2));
    }

}
这是打印结果:

9:02


11:47

您可以将两个日期之间的差值转换为毫秒,然后将其转换为分钟或小时

public static Long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
        long diffInMillies = date1.getTime() - date2.getTime();
        return diffInMillies;
    }

public static void main(String[] args) throws IOException {
    double value=  Test.getDateDiff( new Date(System.currentTimeMillis()+37*10000),  new Date(System.currentTimeMillis()), TimeUnit.MINUTES);
    System.out.println("Hour:"+value/(100*60*60));

}

JDK 8日期-时间API极大地简化了这些操作,可以使用以下持续时间计算时间差:

LocalTime t1 = LocalTime.of(11, 18);
LocalTime t2 = LocalTime.of(20, 20);
System.out.println(Duration.between(t1, t2)); //output - PT9H2M 

输出为9小时2分钟。(9小时2米)

你差点就拿到了。计算你所需要的分钟数

long minutes = d / (60*1000) % 60;
long seconds = d / 1000 % 60;

我加了秒以防万一。虽然我有点喜欢@Pallavi的答案,使用Duration,但从结果对象中提取实际数据更为人为。

如果不使用JDK 8,可以使用JodaTime,但使用JDK 8可以编写如下内容:

public static void main(String[] args) {
    LocalDateTime from = LocalDateTime.of(2017, 2, 5, 11, 18, 0);
    LocalDateTime to = LocalDateTime.of(2017, 2, 5, 20, 20, 0);

    long seconds = ChronoUnit.SECONDS.between(from, to);
    System.out.println(String.format("%d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, (seconds % 60)));
}
输出时间为:9:02:00

要获得两个LocalDateTime之间的差异,您可以使用Duration类,但是JDK 8中没有用于它的格式化程序,因此如果您需要这种特定的输出格式,那么您可以使用第三方库,或者编写自己的格式化程序,如上面示例中的格式化程序

要使用持续时间,您可以编写:

public static void main(String[] args) {  
    LocalDateTime from = LocalDateTime.of(2017, 2, 5, 11, 18, 0);  
    LocalDateTime to = LocalDateTime.of(2017, 2, 5, 20, 20, 0);

    Duration duration = Duration.between(from, to);

    long seconds = duration.getSeconds();
    System.out.println(String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, (s % 60))); 
}

干杯,A.

好吧,你有计算时间的代码。。。这几分钟你都试了些什么?(提示:不要除以60的次数…)等等,你的时间在
h1
m1
h2
m2
那么你为什么不使用这些值来计算呢?嗯,我试过了,一开始就把数学搞错了@AxelH,这就是为什么切换到这种格式谢谢我理解你的意思。。!!长dmin=d/(60*1000)%60;再次感谢:)@JonSkeet@Saurabh你可以尝试更多,是的,这里有一些技巧,因为在某些情况下你需要减去一个小时。但是你可以简单地把小时转换成分钟,把它加到分钟里,再做数学运算,重新创建小时:分钟格式。