簡體   English   中英

需要以hh:mm格式獲取兩個日期之間的時差

[英]Need to get the time difference between the two dates in hh:mm format

我需要以HH:MM格式獲取不同日期之間的時差。假設我有兩個這樣的日期

2014年2月26日09:00:00和2014年2月26日19:30:00

我需要像09:30一樣得到hh:mm的差異。

我用谷歌搜索並試圖找到解決方案,但是他們給出了個別的時間和分鍾。

我不允許使用像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);  
    } 

}

  1. 使用SimpleDateFormat將時間戳解析為Date
  2. 計算dateOne.getTime()dateTwo.getTime()之間的差異。 結果將以毫秒為單位。
  3. 使用TimeUnit實例將毫秒轉換為小時和分鍾。

嘗試這個

    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

我的解決方案:

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

}

請記住,約會並不像您期望的那么容易。 有leap秒和各種奇怪的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM