获得2次持续时间

时间:2013-08-08 06:53:15

标签: java calendar

如何以YYYYMMDDTHHMMSS格式获取两个字符串的持续时间?

我正在尝试使用Calendar类并检查getTimeInMillis()。我遇到的问题是它不一致。知道我做错了什么吗? 每次我运行这个程序时,我都会得到40-70行输出到控制台,而它应该没有。

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++ +")");
            }
        }
    }
}

5 个答案:

答案 0 :(得分:2)

您可以使用SimpleDateFormat API来解析您的String:

public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
    try {
        Date date1 = sdf.parse("20130108T150000");
        Date date2 = sdf.parse("20130108T131500");
        System.out.println((date1.getTime() - date2.getTime())/1000/60);
    } catch (ParseException e) {
       e.printStackTrace();
    }
}

答案 1 :(得分:2)

那是documented bug

尝试在设置之前清除日历:

cal1.clear();
cal1.set(yyyy1, mm1, dd1, hh1, min1, 0);

cal2.clear();
cal2.set(yyyy2, mm2, dd2, hh2, min2, 0);

答案 2 :(得分:1)

试试这个

    String t1 = "20130108T150000";
    String t2 = "20130108T131500";
    DateFormat df=new SimpleDateFormat("yyyyMMdd'T'HHmmss");
    Date date1=df.parse(t1);
    Date date2=df.parse(t2);

    System.out.println(date2.getTime()-date1.getTime());  // time difference in mil-seconds

答案 3 :(得分:1)

我建议使用SimpleDateFormat来解析你的String到日期对象

SimpleDateFormat date_format = new SimpleDateFormat("yyyyMMdd'T'HHmmSS");
Date dateParsed = date_format.parse(yourStringDate)

您可以使用Date.getTime()函数获取以毫秒为单位的日期对象,并执行两个日期对象之间的差异

答案 4 :(得分:1)

首先,您的日期转换已关闭Calendar个月被归零(即Janurary实际上是月0),因此您不是开始Tue Jan 08 15:00:00 EST 2013,而是日期实际上被转换为Fri Feb 08 15:00:00 EST 2013,这本身并不是问题的根源,而是令人担忧的。

相反,你应该使用......

public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd'T'HHmmss");

public static int getDuration(String time1, String time2){
    Date date1 = SDF.parse(time1);
    Date date2 = SDF.parse(time2);

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

String值转换回Date

现在,问题的原因......

由于您使用Calendar设置了set(int year, int month, int date, int hourOfDay, int minute, int second)值,因此日历实例仍然包含创建时的毫秒数(Calendar.getInstance()将返回Calendar集到它创建的日期/时间),但你没有将这些值清零。

这会导致计算错误

您不应该依赖CalendarDate计算持续时间,而是长期不可靠,而应使用JodaTime

DateTime dt1 = new DateTime(date1);
DateTime dt2 = new DateTime(date2);

Duration duration = new Duration(dt2, dt1);
System.out.println(duration.getStandardDays());
System.out.println(duration.getStandardHours());
System.out.println(duration.getStandardMinutes());