两次使用java的时差

时间:2017-04-24 06:24:04

标签: java time

我正在尝试获得两个时间值之间的时差。 (11:00:00和12:43:00)对于上面的输入,它显示正确的输出。

**

Time in seconds: 6180 seconds.
Time in minutes: 103 minutes.
Time in hours: 1 hours.
Time in hours Round : 2 hours.

** 对于输入值(“11:00:00”和“2:43:00”)它应该给我们 3小时作为圆值 **

Time in seconds: -29820 seconds.
Time in minutes: -497 minutes.
Time in hours: -8 hours.
Time in hours Round : -8 hours.

**

import java.lang.Math; 
import java.text.SimpleDateFormat;
import java.util.Date;


public class HelloWorld
{
  public static void main(String[] args)
  {


String dateStart = "11:00:00";
String dateStop = "12:43:00";
// Custom date format
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");  
Date d1 = null;
Date d2 = null;
try {
    d1 = format.parse(dateStart);
    d2 = format.parse(dateStop);
} catch (Exception e) {
    e.printStackTrace();
}    

// Get msec from each, and subtract.
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000;         
long diffMinutes = diff / (60 * 1000);         
long diffHours = diff / (60 * 60 * 1000);              
int parkingEntry=2;
int firstHour=3;
int parkingRate=4;
int Total=0;

System.out.println("Time in seconds: " + diffSeconds + " seconds.");         
System.out.println("Time in minutes: " + diffMinutes + " minutes.");         
System.out.println("Time in hours: " + diffHours + " hours."); 

double my_hrs=diffMinutes/60d;
     int r = (int) Math.round(my_hrs);
System.out.println("Time in hours Round : " + r + " hours."); 


  }
}

2 个答案:

答案 0 :(得分:1)

格式“HH:mm:ss”是24小时格式。因此,当您检查“11:00:00”和“2:43:00”之间的时差时,您实际上是在检查上午11点到凌晨2点43分之间的时差。

将您的输入更改为“11:00:00”和“14:43:00”。

答案 1 :(得分:0)

您的基本问题是您的输入不足以计算差异。您不知道时间是在中午之前还是之后(在上午或下午)。最好的解决方案是在输入中要求此信息。通过使用24小时(已经建议的格式为HH)或者在输入中添加AM / PM标记。

假设你无法改变输入,你当然可以尽力而为。如果停止时间似乎在开始时间之前,则可能是因为开始时间是AM和停止时间PM。请注意,这远非防弹:它仍将计算从6到7的时间为1小时,可能是从早上6点到晚上7点,即13小时。

我将借此机会演示如何使用java.time类:

    // Two formats, for AM and PM
    final String basicTimeFormatPattern = "h:mm:ss";
    DateTimeFormatter amFormat = new DateTimeFormatterBuilder().appendPattern(basicTimeFormatPattern)
            .parseDefaulting(ChronoField.AMPM_OF_DAY, 0)
            .toFormatter();
    DateTimeFormatter pmFormat = new DateTimeFormatterBuilder().appendPattern(basicTimeFormatPattern)
            .parseDefaulting(ChronoField.AMPM_OF_DAY, 1)
            .toFormatter();
    LocalTime t1 = LocalTime.parse(dateStart, amFormat);
    LocalTime t2 = LocalTime.parse(dateStop, amFormat);
    if (t2.isBefore(t1)) {
        // parse in PM instead
        t2 = LocalTime.parse(dateStop, pmFormat);
    }
    Duration diff = Duration.between(t1, t2);
    System.out.println("Time in seconds: " + diff.getSeconds() + " seconds.");         
    long diffMinutes = diff.toMinutes();
    System.out.println("Time in minutes: " + diffMinutes + " minutes.");         
    System.out.println("Time in hours: " + diff.toHours() + " hours.");

代码与您的代码大小相同,但请注意代码行是在问题的困难部分花费,解析不完整的输入并确定AM和PM,而时间单位之间的平凡转换是通过库方法,不要填写我们自己的代码。可以按照问题中的代码计算舍入小时数,因此我将其删除了。

以上版画:

Time in seconds: 6180 seconds.
Time in minutes: 103 minutes.
Time in hours: 1 hours.
Time in hours Round : 2 hours.

dateStop = "2:43:00"

Time in seconds: 13380 seconds.
Time in minutes: 223 minutes.
Time in hours: 3 hours.
Time in hours Round : 4 hours.