nickhumphrey

Calculate the difference between two java.util.Date objects

Jun 11th, 2013
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. package com.takecargo.utils;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import java.util.concurrent.TimeUnit;
  6.  
  7. public class TCDateUtils {
  8.  
  9.     // a general method, independent of unit of time--it's specified as a parameter. see here for possible values: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/TimeUnit.html
  10.     // e.g.: TimeUnit.DAYS, TimeUnit.HOURS
  11.     public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
  12.         long diffInMillies = date2.getTime() - date1.getTime();
  13.         return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
  14.     }
  15.  
  16.     // http://stackoverflow.com/a/10650881/557194
  17.     public static long getDateDiffInDays(Date date1, Date date2) {
  18.         long diffInMillis = date2.getTime() - date1.getTime();
  19.         return TimeUnit.MILLISECONDS.toDays(diffInMillis);  // http://stackoverflow.com/a/7829642/557194
  20.     }
  21.    
  22.     // find out the number of hours (remainder) after the diffInDays value between date1 and date2
  23.     public static int getHoursRemainderFromDateDiffInDays(Date date1, Date date2) {
  24.         Calendar c1 = Calendar.getInstance();
  25.         c1.setTime(date1);
  26.         int hr1 = c1.get(Calendar.HOUR_OF_DAY);
  27.  
  28.         Calendar c2 = Calendar.getInstance();
  29.         c2.setTime(date2);
  30.         int hr2 = c2.get(Calendar.HOUR_OF_DAY);
  31.  
  32.         return Math.abs(hr2-hr1);
  33.     }
  34.  
  35.  
  36.     // find the "to date" calculated from today's date; return value should be equal to date2, above
  37.     // @param days: will come from getDateDiffInDays()
  38.     // @param hours: will come from getHoursRemainderFromDateDiffInDays()
  39.     public static Date getToDateInXDaysAndXHours(long days, int hours) {
  40.         Calendar toDate = Calendar.getInstance();
  41.         toDate.add(Calendar.DAY_OF_YEAR, (int) days);
  42.         toDate.add(Calendar.HOUR_OF_DAY, (int) ++hours);    // ++hours because HOUR_OF_DAY is zero-based
  43.         return toDate.getTime();
  44.     }
  45.    
  46.  
  47.  
  48.     // ########### TEST CODE ###############
  49.  
  50.     public static void main(String[] args) {
  51.         Calendar today = Calendar.getInstance();
  52.         Calendar tomorrow = Calendar.getInstance();
  53.         tomorrow.add(Calendar.DAY_OF_YEAR, 1);
  54.  
  55.         System.out.println(getDateDiffInDays(today.getTime(), tomorrow.getTime())); // OUTPUT: 1
  56.         System.out.println(getDateDiff(today.getTime(), tomorrow.getTime(), TimeUnit.DAYS));    // OUTPUT: 1
  57.         System.out.println(getDateDiff(today.getTime(), tomorrow.getTime(), TimeUnit.HOURS));   // OUTPUT: 24
  58.  
  59.         // add 6 hours to "tomorrow"'s point in time
  60.         tomorrow.add(Calendar.HOUR_OF_DAY, 6);
  61.  
  62.         System.out.println(getDateDiff(today.getTime(), tomorrow.getTime(), TimeUnit.HOURS));   // OUTPUT: 30
  63.  
  64.         Date toDate = getToDateInXDaysAndXHours(
  65.                 getDateDiffInDays(  // value: 1
  66.                     today.getTime(),
  67.                     tomorrow.getTime()
  68.                 ),
  69.                 getHoursRemainderFromDateDiffInDays(    // value: 6
  70.                     today.getTime(),
  71.                     tomorrow.getTime()
  72.                 )
  73.         );
  74.         System.out.println(toDate); // OUTPUT (e.g. today == "Tue Jun 10 11:41:13 CEST 2013"): Tue Jun 11 17:41:13 CEST 2013
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment