需要以hh:mm格式获取两个日期之间的时差

时间:2014-03-05 13:50:16

标签: java date

我需要在HH:MM格式中获取不同日期之间的时差。假设我有两个这样的日期

  

02/26/2014 09:00:00和02/26/2014 19:30:00

我需要得到hh:mm的差异,比如09:30。

我用谷歌搜索并试图找到解决方案,但他们给出了个人的小时和分钟。

我不允许像Joda那样使用第三方库。有谁能指出我正确的方向?

  

更新

我尝试了以下代码

public class DateDifferentExample {

/**
 * @param args
 */
public static void main(String[] args) {

    String dateStart = "02/26/2014 09:00:00";
    String dateStop = "02/26/2014 19:05:00";

    //HH converts hour in 24 hours format (0-23), day calculation
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    Date d1 = null;
    Date d2 = null;

    try {
        d1 = format.parse(dateStart);
        d2 = format.parse(dateStop);

        //in milliseconds
        long diff = d2.getTime() - d1.getTime();

        System.out.println("Time difference-->"+diff);

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        int diffInDays = (int) (d2.getTime() - d1.getTime());

        System.out.println("Difference--> "+diffInDays);
       String difft=diffHours+":"+diffMinutes;
        System.out.println("Duration Time:"+difft);

        /*System.out.print(diffDays + " days, ");
        System.out.print(diffHours + " hours, ");
        System.out.print(diffMinutes + " minutes, ");
        System.out.print(diffSeconds + " seconds.");*/

        //System.out.println("Getting date diff from the other method--->"+calculateDays(d1, d2));

    } catch (Exception e) {
        e.printStackTrace();
    }


}

/*public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
    long diffInMillies = date2.getTime() - date1.getTime();
    return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
}*/

public static long calculateDays(Date dateEarly, Date dateLater) {  
       return (dateLater.getTime() - dateEarly.getTime()) / (24 * 60 * 60 * 1000);  
    } 

}

3 个答案:

答案 0 :(得分:1)

  1. 使用Date将时间戳解析为SimpleDateFormat
  2. 计算dateOne.getTime()dateTwo.getTime()之间的差异。结果将是以毫秒为单位的差异。
  3. 使用TimeUnit实例将毫秒转换为小时和分钟。

答案 1 :(得分:1)

试试这个

    SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    Date d1 = df.parse("02/26/2014 09:00:00");
    Date d2 = df.parse("02/26/2014 19:30:00");
    long d = d2.getTime() - d1.getTime();
    long hh = d / (3600 * 1000);
    long mm = (d - hh * 3600 * 1000) / (60 * 1000);
    System.out.printf("%02d:%02d", hh, mm);

打印

10:30

答案 2 :(得分:1)

我的解决方案:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeDiff {

    public static void main(String[] args) throws ParseException {

        // Setup
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "MM/dd/yyyy HH:mm:ss");
        long second = 1000l;
        long minute = 60l * second;
        long hour = 60l * minute;

        // parsing input
        Date date1 = dateFormat.parse("02/26/2014 09:00:00");
        Date date2 = dateFormat.parse("02/26/2014 19:30:00");

        // calculation
        long diff = date2.getTime() - date1.getTime();

        // printing output
        System.out.print(String.format("%02d", diff / hour));
        System.out.print(":");
        System.out.print(String.format("%02d", (diff % hour) / minute));
        System.out.print(":");
        System.out.print(String.format("%02d", (diff % minute) / second));
    }

}

请记住,日期并不像您期望的那么容易。有闰秒和各种奇怪的东西。