亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在java中找到兩個日期之間的差異持續時間?

如何在java中找到兩個日期之間的差異持續時間?

慕慕森 2019-07-30 15:54:11
如何在java中找到兩個日期之間的差異持續時間?我有兩個DateTime對象,需要找到它們的差異持續時間,我有以下代碼,但不知道如何繼續它以獲得預期的結果如下:例      11/03/14 09:30:58      11/03/14 09:33:43      elapsed time is 02 minutes and 45 seconds      -----------------------------------------------------      11/03/14 09:30:58       11/03/15 09:30:58      elapsed time is a day      -----------------------------------------------------      11/03/14 09:30:58       11/03/16 09:30:58      elapsed time is two days      -----------------------------------------------------      11/03/14 09:30:58       11/03/16 09:35:58      elapsed time is two days and 05 mintues碼    String dateStart = "11/03/14 09:29:58";    String dateStop = "11/03/14 09:33:43";    Custom date format    SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");    Date d1 = null;    Date d2 = null;    try {        d1 = format.parse(dateStart);        d2 = format.parse(dateStop);    } catch (ParseException e) {        e.printStackTrace();    }    // Get msec from each, and subtract.    long diff = d2.getTime() - d1.getTime();    long diffSeconds = diff / 1000 % 60;    long diffMinutes = diff / (60 * 1000) % 60;    long diffHours = diff / (60 * 60 * 1000);    System.out.println("Time in seconds: " + diffSeconds + " seconds.");    System.out.println("Time in minutes: " + diffMinutes + " minutes.");    System.out.println("Time in hours: " + diffHours + " hours.");
查看完整描述

3 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

嘗試以下方法

{
        Date dt2 = new DateAndTime().getCurrentDateTime();

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

        if (diffInDays > 1) {
            System.err.println("Difference in number of days (2) : " + diffInDays);
            return false;
        } else if (diffHours > 24) {

            System.err.println(">24");
            return false;
        } else if ((diffHours == 24) && (diffMinutes >= 1)) {
            System.err.println("minutes");
            return false;
        }
        return true;}


查看完整回答
反對 回復 2019-07-30
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

可以使用Java內置類TimeUnit以更好的方式處理日期差異轉換。它提供了實用方法:

Date startDate = // Set start date

Date endDate   = // Set end date


long duration  = endDate.getTime() - startDate.getTime();


long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);

long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);

long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);

long diffInDays = TimeUnit.MILLISECONDS.toDays(duration);


查看完整回答
反對 回復 2019-07-30
?
茅侃侃

TA貢獻1842條經驗 獲得超22個贊

使用Joda-Time

DateTime startTime, endTime;Period p = new Period(startTime, endTime);long hours = p.getHours();long minutes = p.getMinutes();

Joda Time有一個時間間隔的概念:

Interval interval = new Interval(oldTime, new Instant());

另一個例子 日期差異

還有一個鏈接

或者使用Java-8(集成了Joda-Time概念)

Instant start, end;//Duration dur = Duration.between(start, stop);long hours = dur.toHours();long minutes = dur.toMinutes();


查看完整回答
反對 回復 2019-07-30
  • 3 回答
  • 0 關注
  • 447 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號