web-dev-qa-db-ja.com

Javaで日付/時刻差を計算する

2つの日付間の差を計算します時間/分/秒単位.

私は私のコードにわずかな問題があります。

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;         
long diffMinutes = diff / (60 * 1000);         
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."); 

これは作り出すべきです:

Time in seconds: 45 seconds.
Time in minutes: 3 minutes.
Time in hours: 0 hours.

しかし、私はこの結果を得ます:

Time in seconds: 225 seconds.
Time in minutes: 3 minutes.
Time in hours: 0 hours.

誰かが私がここで間違っていることを見ることができますか?

140
London

やってみる

long diffSeconds = diff / 1000 % 60;  
long diffMinutes = diff / (60 * 1000) % 60; 
long diffHours = diff / (60 * 60 * 1000);

注:これはdiffが負ではないと仮定しています。

93
Peter Lawrey

私は提案されたJava.util.concurrent.TimeUnitクラスを使うのを好むでしょう。

long diff = d2.getTime() - d1.getTime();//as given

long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff); 
209
Admit

もしあなたが外部ライブラリを使うことができるなら、私はあなたが Joda-Time を使うことを勧めます。

Joda-Timeは、Java SE 8より前のJava用のデファクトスタンダードの日時ライブラリです。

間計算の例

Seconds.between(startDate, endDate);
Days.between(startDate, endDate);
39
heldt

Java 5以降、 Java.util.concurrent.TimeUnit を使用して、コード内で1000や60などのマジックナンバーを使用しないようにできます。

ちなみに、あなたはあなたの計算の中で秒をうるうするように気をつけるべきです:年の最後の分は追加のうるう秒を持つかもしれないのでそれは確かに予想される60秒の代わりに61秒続きます。 ISOの仕様では、おそらく61秒も予定しています。詳細は Java.util.Date javadocにあります。

18
Yves Martin

時間差(ミリ秒)をわかりやすく表現するためにこれを試してください。

String friendlyTimeDiff(long timeDifferenceMilliseconds) {
    long diffSeconds = timeDifferenceMilliseconds / 1000;
    long diffMinutes = timeDifferenceMilliseconds / (60 * 1000);
    long diffHours = timeDifferenceMilliseconds / (60 * 60 * 1000);
    long diffDays = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24);
    long diffWeeks = timeDifferenceMilliseconds / (60 * 60 * 1000 * 24 * 7);
    long diffMonths = (long) (timeDifferenceMilliseconds / (60 * 60 * 1000 * 24 * 30.41666666));
    long diffYears = timeDifferenceMilliseconds / ((long)60 * 60 * 1000 * 24 * 365);

    if (diffSeconds < 1) {
        return "less than a second";
    } else if (diffMinutes < 1) {
        return diffSeconds + " seconds";
    } else if (diffHours < 1) {
        return diffMinutes + " minutes";
    } else if (diffDays < 1) {
        return diffHours + " hours";
    } else if (diffWeeks < 1) {
        return diffDays + " days";
    } else if (diffMonths < 1) {
        return diffWeeks + " weeks";
    } else if (diffYears < 1) {
        return diffMonths + " months";
    } else {
        return diffYears + " years";
    }
}
12
Yogev

これは基本的にJavaの問題よりも数学的な問題です。

あなたが受け取る結果は正しいです。これは、225秒が3分であるためです(整数除算の場合)。あなたが欲しいものはこれです:

  • 秒数を得るために1000で割る - > restはミリ秒
  • 分数を得るためにそれを60で割る - >休息は秒
  • それを60で割って時間数を求めます - >残り時間は分です

またはJavaの場合:

int millis = diff % 1000;
diff/=1000;
int seconds = diff % 60;
diff/=60;
int minutes = diff % 60;
diff/=60;
hours = diff;
6
Joeri Hendrickx

これは、各時間部分を取得してフォーマットするためのTimeUnitを使用した提案です。

private static String formatDuration(long duration) {
    long hours = TimeUnit.MILLISECONDS.toHours(duration);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) % 60;
    long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) % 60;
    long milliseconds = duration % 1000;
    return String.format("%02d:%02d:%02d,%03d", hours, minutes, seconds, milliseconds);
}

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss,SSS");
Date startTime = sdf.parse("01:00:22,427");
Date now = sdf.parse("02:06:38,355");
long duration = now.getTime() - startTime.getTime();
System.out.println(formatDuration(duration));

年です。結果:01:06:15,928

3
Jose Tepedino

Javaにおける2つの日付間の差

リンクからコードを抽出した

public class TimeDiff {
    /**
     * (For testing purposes)
     *
     */
    public static void main(String[] args) {
        Date d1 = new Date();
        try { Thread.sleep(750); } catch(InterruptedException e) { /* ignore */ }      
        Date d0 = new Date(System.currentTimeMillis() - (1000*60*60*24*3)); // About 3 days ago
        long[] diff = TimeDiff.getTimeDifference(d0, d1);

        System.out.printf("Time difference is %d day(s), %d hour(s), %d minute(s), %d second(s) and %d millisecond(s)\n",
                diff[0], diff[1], diff[2], diff[3], diff[4]);
        System.out.printf("Just the number of days = %d\n",
                TimeDiff.getTimeDifference(d0, d1, TimeDiff.TimeField.DAY));
    }

    /**
     * Calculate the absolute difference between two Date without
     * regard for time offsets
     *
     * @param d1 Date one
     * @param d2 Date two
     * @param field The field we're interested in out of
     * day, hour, minute, second, millisecond
     *
     * @return The value of the required field
     */
    public static long getTimeDifference(Date d1, Date d2, TimeField field) {
        return TimeDiff.getTimeDifference(d1, d2)[field.ordinal()];
    }

    /**
     * Calculate the absolute difference between two Date without
     * regard for time offsets
     *
     * @param d1 Date one
     * @param d2 Date two
     * @return The fields day, hour, minute, second and millisecond
     */
    public static long[] getTimeDifference(Date d1, Date d2) {
        long[] result = new long[5];
        Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("UTC"));
        cal.setTime(d1);

        long t1 = cal.getTimeInMillis();
        cal.setTime(d2);

        long diff = Math.abs(cal.getTimeInMillis() - t1);
        final int ONE_DAY = 1000 * 60 * 60 * 24;
        final int ONE_HOUR = ONE_DAY / 24;
        final int ONE_MINUTE = ONE_HOUR / 60;
        final int ONE_SECOND = ONE_MINUTE / 60;

        long d = diff / ONE_DAY;
        diff %= ONE_DAY;

        long h = diff / ONE_HOUR;
        diff %= ONE_HOUR;

        long m = diff / ONE_MINUTE;
        diff %= ONE_MINUTE;

        long s = diff / ONE_SECOND;
        long ms = diff % ONE_SECOND;
        result[0] = d;
        result[1] = h;
        result[2] = m;
        result[3] = s;
        result[4] = ms;

        return result;
    }

    public static void printDiffs(long[] diffs) {
        System.out.printf("Days:         %3d\n", diffs[0]);
        System.out.printf("Hours:        %3d\n", diffs[1]);
        System.out.printf("Minutes:      %3d\n", diffs[2]);
        System.out.printf("Seconds:      %3d\n", diffs[3]);
        System.out.printf("Milliseconds: %3d\n", diffs[4]);
    }

    public static enum TimeField {DAY,
        HOUR,
        MINUTE,
        SECOND,
        MILLISECOND;
    }
}
3
Dead Programmer

私はこれが古い質問であることを知っています、しかし私は受け入れられた答えとはわずかに違う何かをすることになった。人々はTimeUnitクラスについて話しますが、OPが望んでいた方法でこれを使った答えはありませんでした。

それで、ここに別の解決策があります、誰かがそれを見逃して来るならば;-)

public class DateTesting {
    public static void main(String[] args) {
        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 days = TimeUnit.MILLISECONDS.toDays(diff);
        long remainingHoursInMillis = diff - TimeUnit.DAYS.toMillis(days);
        long hours = TimeUnit.MILLISECONDS.toHours(remainingHoursInMillis);
        long remainingMinutesInMillis = remainingHoursInMillis - TimeUnit.HOURS.toMillis(hours);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(remainingMinutesInMillis);
        long remainingSecondsInMillis = remainingMinutesInMillis - TimeUnit.MINUTES.toMillis(minutes);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(remainingSecondsInMillis);

        System.out.println("Days: " + days + ", hours: " + hours + ", minutes: " + minutes + ", seconds: " + seconds);
    }
}

違いを自分で計算するだけでもできますが、そのようにしても意味がありません。TimeUnitは見過ごされがちなクラスです。

3
Darwind

ジョダタイム

Joda-Time 2.3ライブラリはこの雑用のためにデバッグ済みのコードを提供しています。

Joad-Timeには、期間を表す3つのクラス、 PeriodInterval、およびDurationがあります。 Periodは、スパンを月数、日数、時間数などで追跡します(タイムラインには関係ありません)。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.

// Specify a time zone rather than rely on default.
// Necessary to handle Daylight Saving Time (DST) and other anomalies.
DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );

DateTimeFormatter formatter = DateTimeFormat.forPattern( "yy/MM/dd HH:mm:ss" ).withZone( timeZone ); 

DateTime dateTimeStart = formatter.parseDateTime( "11/03/14 09:29:58" );
DateTime dateTimeStop = formatter.parseDateTime( "11/03/14 09:33:43" );
Period period = new Period( dateTimeStart, dateTimeStop );

PeriodFormatter periodFormatter = PeriodFormat.getDefault();
String output = periodFormatter.print( period );

System.out.println( "output: " + output );

実行すると…

output: 3 minutes and 45 seconds
2
Basil Bourque
// d1, d2 are dates
long diff = d2.getTime() - d1.getTime();

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

System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
2
Fizer Khan

コンストラクタとしてあなたの時間の間の違いを使ってDateオブジェクトを作成します、
それから値を得るためにカレンダーメソッドを使います..

Date diff = new Date(d2.getTime() - d1.getTime());

Calendar calendar = Calendar.getInstance();
calendar.setTime(diff);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
int seconds = calendar.get(Calendar.SECOND);
2
bobbyrne01

これが私のコードです。

import Java.util.Date;

// to calculate difference between two days
public class DateDifference {

// to calculate difference between two dates in milliseconds
public long getDateDiffInMsec(Date da, Date db) {
    long diffMSec = 0;
    diffMSec = db.getTime() - da.getTime();
    return diffMSec;
}

// to convert Milliseconds into DD HH:MM:SS format.
public String getDateFromMsec(long diffMSec) {
    int left = 0;
    int ss = 0;
    int mm = 0;
    int hh = 0;
    int dd = 0;
    left = (int) (diffMSec / 1000);
    ss = left % 60;
    left = (int) left / 60;
    if (left > 0) {
        mm = left % 60;
        left = (int) left / 60;
        if (left > 0) {
            hh = left % 24;
            left = (int) left / 24;
            if (left > 0) {
                dd = left;
            }
        }
    }
    String diff = Integer.toString(dd) + " " + Integer.toString(hh) + ":"
            + Integer.toString(mm) + ":" + Integer.toString(ss);
    return diff;

}
}
1
Souji
Date startTime = new Date();
//...
//... lengthy jobs
//...
Date endTime = new Date();
long diff = endTime.getTime() - startTime.getTime();
String hrDateText = DurationFormatUtils.formatDuration(diff, "d 'day(s)' H 'hour(s)' m 'minute(s)' s 'second(s)' ");
System.out.println("Duration : " + hrDateText);


Apache Commons Duration Format Utils を使用できます。フォーマットは SimpleDateFormatter のようになります。

出力:

0 days(s) 0 hour(s) 0 minute(s) 1 second(s)

0

前述のように - これは良い答えだと思います

/**
 * @param d2 the later date 
 * @param d1 the earlier date
 * @param timeUnit - Example Calendar.HOUR_OF_DAY
 * @return
 */
public static int getTimeDifference(Date d2,Date d1, int timeUnit) {
     Date diff = new Date(d2.getTime() - d1.getTime());

     Calendar calendar = Calendar.getInstance();
     calendar.setTime(diff);
     int hours = calendar.get(Calendar.HOUR_OF_DAY);
     int minutes = calendar.get(Calendar.MINUTE);
     int seconds = calendar.get(Calendar.SECOND);
     if(timeUnit==Calendar.HOUR_OF_DAY)
         return hours;
     if(timeUnit==Calendar.MINUTE)
         return minutes;
     return seconds;
 }
0
yonia

それでは、もう1つのコードサンプルを試してみます。

/**
 * Calculates the number of FULL days between to dates
 * @param startDate must be before endDate
 * @param endDate must be after startDate
 * @return number of day between startDate and endDate
 */
public static int daysBetween(Calendar startDate, Calendar endDate) {
    long start = startDate.getTimeInMillis();
    long end = endDate.getTimeInMillis();
    // It's only approximation due to several bugs (@see Java.util.Date) and different precision in Calendar chosen
    // by user (ex. day is time-quantum).
    int presumedDays = (int) TimeUnit.MILLISECONDS.toDays(end - start);
    startDate.add(Calendar.DAY_OF_MONTH, presumedDays);
    // if we still didn't reach endDate try it with the step of one day
    if (startDate.before(endDate)) {
        startDate.add(Calendar.DAY_OF_MONTH, 1);
        ++presumedDays;
    }
    // if we crossed endDate then we must go back, because the boundary day haven't completed yet
    if (startDate.after(endDate)) {
        --presumedDays;
    }
    return presumedDays;
}
0
krems

long diffSeconds =(diff/1000)%60。
これを試して、正しく機能するかどうかを教えてください。

0
Akshatha