获得2次的持续时间 [英] Geting duration of 2 times
本文介绍了获得2次的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何获取两个字符串的持续时间,格式为 YYYYMMDDTHHMMSS
?
使用Calendar类并检查 getTimeInMillis()
。问题我有这个是,它是不一致的。任何想法我做错了什么?
每次我运行这个程序,我得到40-70行输出到控制台,当它应该是无。
public class DurationTester {
/ **
*获取两个给定时间之间的持续时间
* @param time1 yyyymmddThhmmss
* @param time2 yyyymmddThhmmss
* @ return time1和time2之间的分钟
* /
public static int getDuration(String time1,String time2){
int yyyy1 = Integer.parseInt(time1.substring(0,4));
int mm1 = Integer.parseInt(time1.substring(4,6));
int dd1 = Integer.parseInt(time1.substring(6,8));
int hh1 = Integer.parseInt(time1.substring(9,11));
int min1 = Integer.parseInt(time1.substring(11,13));
int yyyy2 = Integer.parseInt(time2.substring(0,4));
int mm2 = Integer.parseInt(time2.substring(4,6));
int dd2 = Integer.parseInt(time2.substring(6,8));
int hh2 = Integer.parseInt(time2.substring(9,11));
int min2 = Integer.parseInt(time2.substring(11,13));
日历cal1 = Calendar.getInstance();
cal1.set(yyyy1,mm1,dd1,hh1,min1,0);
日历cal2 = Calendar.getInstance();
cal2.set(yyyy2,mm2,dd2,hh2,min2,0);
long milliSec = cal1.getTimeInMillis() - cal2.getTimeInMillis();
long nonNegativeMS = Math.abs(milliSec);
long seconds = nonNegativeMS / 1000;
long minutes = seconds / 60;
return(int)minutes;
}
public static void main(String [] args){
String t1 =20130108T150000;
String t2 =20130108T131500;
int errors = 0;
for(int i = 0; i <5000; i ++){
int duration = getDuration(t1,t2);
if(duration == 104){
System.out.println(ERROR:Should only be 105(+ errors ++ +));
}
}
}
}
解决方案
这是一个记录的错误。 / p>
请尝试在设置之前清除日历:
cal1.clear ;
cal1.set(yyyy1,mm1,dd1,hh1,min1,0);
cal2.clear();
cal2.set(yyyy2,mm2,dd2,hh2,min2,0);
How can I get the duration of two Strings in the format YYYYMMDDTHHMMSS
?
I´m trying with using the Calendar class and checking the getTimeInMillis()
. Problem I am having with this is that it is not consistent. Any idea what I am doing wrong?
Every time I run this program I get 40-70 lines of output to console when it should be none.
public class DurationTester {
/**
* Get the duration between two given times
* @param time1 yyyymmddThhmmss
* @param time2 yyyymmddThhmmss
* @return minutes between time1 and time2
*/
public static int getDuration(String time1, String time2){
int yyyy1 = Integer.parseInt(time1.substring(0,4));
int mm1 = Integer.parseInt(time1.substring(4,6));
int dd1 = Integer.parseInt(time1.substring(6,8));
int hh1 = Integer.parseInt(time1.substring(9,11));
int min1 = Integer.parseInt(time1.substring(11,13));
int yyyy2 = Integer.parseInt(time2.substring(0,4));
int mm2 = Integer.parseInt(time2.substring(4,6));
int dd2 = Integer.parseInt(time2.substring(6,8));
int hh2 = Integer.parseInt(time2.substring(9,11));
int min2 = Integer.parseInt(time2.substring(11,13));
Calendar cal1 = Calendar.getInstance();
cal1.set(yyyy1, mm1, dd1, hh1, min1, 0);
Calendar cal2 = Calendar.getInstance();
cal2.set(yyyy2, mm2, dd2, hh2, min2, 0);
long milliSec = cal1.getTimeInMillis()-cal2.getTimeInMillis();
long nonNegativeMS = Math.abs(milliSec);
long seconds = nonNegativeMS / 1000;
long minutes = seconds / 60;
return (int)minutes;
}
public static void main(String[] args){
String t1 = "20130108T150000";
String t2 = "20130108T131500";
int errors = 0;
for(int i=0; i<5000; i++){
int duration = getDuration(t1,t2);
if(duration == 104){
System.out.println("ERROR: Should only be 105 ("+errors++ +")");
}
}
}
}
解决方案
That's a documented bug.
Try clearing the calendar before set:
cal1.clear();
cal1.set(yyyy1, mm1, dd1, hh1, min1, 0);
cal2.clear();
cal2.set(yyyy2, mm2, dd2, hh2, min2, 0);
这篇关于获得2次的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文