我无法找到我的错误,它在同一天完成了 但是当我输入一个不同的日期时,它会进行大量的计算。 它是我试图做的停车场软件。计算两小时之间的时间
public void obtenerSalida(String fech, String fech_s,String hora_sal,String serie,String hora_e,
javax.swing.JTextField periodo){
String per;
String dateStart =fech+" "+hora_e;
String dateStop =fech_s+" "+hora_sal;
System.out.println(dateStart);
System.out.println(dateStop);
SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd 00:00:00");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
} catch (ParseException e) {
e.printStackTrace();
}
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000%60;
long diffMinutes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
if((diffMinutes>=1) && (diffMinutes<60)){
periodo.setText(diffMinutes + " minuto(s).");
}
else
if((diffMinutes>=60)&&(diffHours>=1 && diffHours<10)){
per=diffHours+":"+(diffMinutes%60);
periodo.setText(per);
}
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");
System.out.println(""+diff);
}
实施例
I enter this values
fech = 6/12/2016
fech_s = 7/12/2016
hora_e = 1:41:49
hora_s = 8:18:43
我得到了这个
6/12/2016 1:41:49
7/12/2016 8:18:43
Time in seconds: 54 seconds.
Time in minutes: 525996 minutes.
Time in hours: 8766 hours.
我知道这完全错了。它不可能这么久
答案 0 :(得分:0)
查看您的日期和日期格式:
SimpleDateFormat("yy/MM/dd 00:00:00")
但您将日期换成其他格式。 而是使用格式:
"d/MM/yyyy HH:mm:ss"