org.clazzes.util.datetime
Class CalendarHelper

java.lang.Object
  extended by org.clazzes.util.datetime.CalendarHelper

public abstract class CalendarHelper
extends java.lang.Object

Static helper functions for calendar objects, which overcome the most painful omissions in the Calendar API.

Author:
wglas

Field Summary
static java.util.Comparator<java.util.Calendar> DATE_ONLY_COMPARATOR
          A comparator instance which compares calendar by their date information only.
 
Constructor Summary
CalendarHelper()
           
 
Method Summary
static java.util.Calendar cloneToFirstDayOfWeek(java.util.Calendar c)
          This function returns a copy of the given calendar, which lies on the first day of the week, where the first day of the week is determined by Calendar.getFirstDayOfWeek(), which reflects the locale-specific settings of the given calendar.
static java.util.Calendar cloneToFirstDayOfWeek(java.util.Calendar c, int firstDayOfWeek)
          This function returns a copy of the given calendar, which lies on the first day of the week.
static int daysBetween(java.util.Calendar c1, java.util.Calendar c2)
          Calculate the number of days between the two given dates.
static int monthsBetween(java.util.Calendar c1, java.util.Calendar c2)
          Calculate the number of months between the two given dates.
static void moveToFirstDayOfWeek(java.util.Calendar c)
          This function move the given calendar to first day of the week, where the first day of the week is determined by Calendar.getFirstDayOfWeek(), which reflects the locale-specific settings of the given calendar.
static void moveToFirstDayOfWeek(java.util.Calendar c, int firstDayOfWeek)
           
static int weeksBetween(java.util.Calendar c1, java.util.Calendar c2, int firstDayOfWeek)
          Calculate the number of weeks between the two given dates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATE_ONLY_COMPARATOR

public static java.util.Comparator<java.util.Calendar> DATE_ONLY_COMPARATOR
A comparator instance which compares calendar by their date information only. Time and time zone information is ignored.

Constructor Detail

CalendarHelper

public CalendarHelper()
Method Detail

daysBetween

public static int daysBetween(java.util.Calendar c1,
                              java.util.Calendar c2)
Calculate the number of days between the two given dates.

Parameters:
c1 - The start date.
c2 - The end date.
Returns:
The number of days between c1 and c2.

weeksBetween

public static int weeksBetween(java.util.Calendar c1,
                               java.util.Calendar c2,
                               int firstDayOfWeek)
Calculate the number of weeks between the two given dates. The number of weeks is actually is calculated by means of the last day d1 with d1.get(Calendar.DAY_OF_WEEK) == firstDayInWeek equal to or before the dates c1 and the last day d2 with d2.get(Calendar.DAY_OF_WEEK) == firstDayInWeek equal to or before the dates c1. The number of weeks is then given by daysBetween(d1,d2)/7.

Parameters:
c1 - The start date.
c2 - The end date.
firstDayOfWeek - The first day of the week as one of the constants Calendar.SUNDAY, ..., Calendar.SATURDAY..
Returns:
The number of days between c1 and c2.

moveToFirstDayOfWeek

public static void moveToFirstDayOfWeek(java.util.Calendar c,
                                        int firstDayOfWeek)
Parameters:
c - The calendar to move to the first day of week.
firstDayOfWeek - The first day in the week as one of the constants Calendar.SUNDAY, ..., Calendar.SATURDAY.

moveToFirstDayOfWeek

public static void moveToFirstDayOfWeek(java.util.Calendar c)
This function move the given calendar to first day of the week, where the first day of the week is determined by Calendar.getFirstDayOfWeek(), which reflects the locale-specific settings of the given calendar.

Parameters:
c - The calendar to move to the first day of week.

cloneToFirstDayOfWeek

public static java.util.Calendar cloneToFirstDayOfWeek(java.util.Calendar c,
                                                       int firstDayOfWeek)
This function returns a copy of the given calendar, which lies on the first day of the week.

Parameters:
c - The input calendar, which is not modified.
firstDayOfWeek - The first day in the week as one of the constants Calendar.SUNDAY, ..., Calendar.SATURDAY.
Returns:
A modified deep copy r of c with r.get(Calendar.DAY_OF_WEEK)==firstDayInWeek.

cloneToFirstDayOfWeek

public static java.util.Calendar cloneToFirstDayOfWeek(java.util.Calendar c)
This function returns a copy of the given calendar, which lies on the first day of the week, where the first day of the week is determined by Calendar.getFirstDayOfWeek(), which reflects the locale-specific settings of the given calendar.

Parameters:
c - The input calendar, which is not modified.
Returns:
A modified deep copy r of c with r.get(Calendar.DAY_OF_WEEK)==firstDayInWeek.

monthsBetween

public static int monthsBetween(java.util.Calendar c1,
                                java.util.Calendar c2)
Calculate the number of months between the two given dates. The number of months is actually is calculated by means of the Calendar.MONTH and Calendar.YEAR fields.

Parameters:
c1 - The start date.
c2 - The end date.
Returns:
The number of months between c1 and c2.


Copyright © 2010. All Rights Reserved.