DateUtils.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package com.goafanti.common.utils;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
  5. /**
  6. * Determines how two dates compare up to no more than the specified most
  7. * significant field.
  8. *
  9. * @param date1
  10. * the first date, not <code>null</code>
  11. * @param date2
  12. * the second date, not <code>null</code>
  13. * @param field
  14. * the field from <code>Calendar</code>
  15. * @return diff millis
  16. * @throws IllegalArgumentException
  17. * if any argument is <code>null</code>
  18. * @see #truncate(Calendar, int)
  19. * @see #truncatedCompareTo(Date, Date, int)
  20. * @since 3.0
  21. */
  22. public static long truncatedDiffTo(final Date date1, final Date date2, final int field) {
  23. final Date truncatedDate1 = truncate(date1, field);
  24. final Date truncatedDate2 = truncate(date2, field);
  25. long thisTime = truncatedDate1.getTime();
  26. long anotherTime = truncatedDate2.getTime();
  27. return Math.abs(thisTime - anotherTime);
  28. }
  29. /**
  30. * Determines how two dates compare up to no more than the specified most
  31. * significant field.
  32. *
  33. * @param date1
  34. * the first date, not <code>null</code>
  35. * @param date2
  36. * the second date, not <code>null</code>
  37. * @param field
  38. * the field from <code>Calendar</code>
  39. * @return diff millis
  40. * @throws IllegalArgumentException
  41. * if any argument is <code>null</code>
  42. * @see #truncate(Calendar, int)
  43. * @see #truncatedCompareTo(Date, Date, int)
  44. * @since 3.0
  45. */
  46. public static long truncatedHourDiffTo(final Date date1, final Date date2) {
  47. return truncatedDiffTo(date1, date2, Calendar.HOUR) / 3600000;
  48. }
  49. }