计算差异两次之间,因为我们有两个不同的字符串的时间和日期 [英] Calculate diff. between two times given that we have two different strings for time and date
本文介绍了计算差异两次之间,因为我们有两个不同的字符串的时间和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算差异。在Java中这样两次。
例如
- 时间1:26/02/2011和11:00 AM
- 时间2:27/02/2011和12:15 AM
差异将是13小时15分钟。 / p>
解决方案
String str_date1 =26/02/2011;
String str_time1 =11:00 AM;
String str_date2 =27/02/2011;
String str_time2 =12:15 AM;
DateFormat formatter = new SimpleDateFormat(dd / MM / yyyy hh:mm a);
日期date1 = formatter.parse(str_date1 ++ str_time1);
日期date2 = formatter.parse(str_date2 ++ str_time2);
//从每个获取msec,并减去。
long diff = date2.getTime() - date1.getTime();
System.out.println(Difference In Days:+(diff /(1000 * 60 * 60 * 24)));
Obs:这只适用于aproximation。请参阅花园路径上的时间丢失。)
I have time data split in two strings - one string for date, and one for time.
I want to calculate the diff. of such two times in Java.
e.g.
- time 1:"26/02/2011" and "11:00 AM"
- time 2:"27/02/2011" and "12:15 AM"
Difference would be like 13 hours 15 minutes.
解决方案
String str_date1 = "26/02/2011";
String str_time1 = "11:00 AM";
String str_date2 = "27/02/2011";
String str_time2 = "12:15 AM" ;
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
Date date1 = formatter.parse(str_date1 + " " + str_time1);
Date date2 = formatter.parse(str_date2 + " " + str_time2);
// Get msec from each, and subtract.
long diff = date2.getTime() - date1.getTime();
System.out.println("Difference In Days: " + (diff / (1000 * 60 * 60 * 24)));
Obs: This is only valid as an aproximation. See Losing Time on the Garden Path.)
这篇关于计算差异两次之间,因为我们有两个不同的字符串的时间和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文