devlights
1/16/2013 - 6:01 AM

Javaで日付の差を取得する方法 (from stackoverflow) http://stackoverflow.com/questions/3951988/java-format-detailed-difference-between-dates

Javaで日付の差を取得する方法 (from stackoverflow) http://stackoverflow.com/questions/3951988/java-format-detailed-difference-between-dates

import java.lang.*;

/**
 * from http://stackoverflow.com/questions/3951988/java-format-detailed-difference-between-dates
 */
public class DiffDate {

  public static void main(String[] args) {
    Date start = new Date(2010, 10, 13);
    Date end   = new Date(2010, 10, 18);

    long diffInMillis = end.getTime() - start.getTime();

    long diffInDays  = diffInMillis/1000/86400;
    long diffInHours = (diffInMillis/1000 - 86400*diffInDays) / 3600;
    long diffInMins  = (diffInMillis/1000 - 86400*diffInDays - 3600*diffInHours) / 60;
    long diffInSecs  = (diffInMillis/1000 - 86400*diffInDays - 3600*diffInHours - 60*diffInMins);

    System.out.println(diffInDays);
    System.out.println(diffInHours);
    System.out.println(diffInMins);
    System.out.println(diffInSecs);
  }
}