001package bradleyross.library.helpers;
002import java.util.Date;
003import java.util.Calendar;
004import java.util.GregorianCalendar;
005/**
006 * Utility functions for processing dates and times.
007 * @author Bradley Ross
008 *
009 */
010public class DateHelpers 
011{
012/**
013 * Since all of the methods are static, the constructor should never
014 * be used.
015 */
016protected DateHelpers()
017{ ; }
018/**
019 * Return the start of a month containing a specified time
020 * @param value Specified time
021 * @return Start of month
022 * 
023 */
024public static Date startOfMonth(Date value)
025{
026        Calendar working = new GregorianCalendar();
027        working.setTime(value);
028        working.set(Calendar.DAY_OF_MONTH, 1);
029        working.set(Calendar.HOUR_OF_DAY, 0);
030        working.set(Calendar.MINUTE, 0);
031        working.set(Calendar.SECOND, 0);
032        working.set(Calendar.MILLISECOND, 0);
033        return working.getTime();
034}       
035/**
036 * Return the start of a day containing a specified time
037 * @param value Specified time
038 * @return Start of day
039 */
040public static Date startOfDay(Date value)
041{
042        Calendar working = new GregorianCalendar();
043        working.setTime(value);
044        working.set(Calendar.HOUR_OF_DAY, 0);
045        working.set(Calendar.MINUTE, 0);
046        working.set(Calendar.SECOND, 0);
047        working.set(Calendar.MILLISECOND, 0);
048        return working.getTime();
049}
050/**
051 * Return the end of a day containing a specified time
052 * @param value Specified time
053 * @return Start of day
054 */
055public static Date endOfDay(Date value)
056{
057        Calendar working = new GregorianCalendar();
058        working.setTime(value);
059        working.set(Calendar.HOUR_OF_DAY, 23);
060        working.set(Calendar.MINUTE, 59);
061        working.set(Calendar.SECOND, 59);
062        working.set(Calendar.MILLISECOND, 999);
063        return working.getTime();
064}
065/**
066 * Return the start of an hour containing a specified time
067 * @param value Specified time
068 * @return Start of hour
069 */
070public static Date startOfHour(Date value)
071{
072        Calendar working = new GregorianCalendar();
073        working.setTime(value);
074        working.set(Calendar.MINUTE, 0);
075        working.set(Calendar.SECOND, 0);
076        working.set(Calendar.MILLISECOND, 0);
077        return working.getTime();
078}
079/**
080 * Return the end of an hour containing a specified time
081 * @param value Specified time
082 * @return End of hour
083 */
084public static Date endOfHour(Date value)
085{
086        Calendar working = new GregorianCalendar();
087        working.setTime(value);
088        working.set(Calendar.MINUTE, 59);
089        working.set(Calendar.SECOND, 59);
090        working.set(Calendar.MILLISECOND, 999);
091        return working.getTime();
092}
093
094/**
095 * Return the start of a minute containing a specified time
096 * @param value Specified time
097 * @return Start of minute
098 */
099public static Date startOfMinute(Date value)
100{
101        Calendar working = new GregorianCalendar();
102        working.setTime(value);
103        working.set(Calendar.SECOND, 0);
104        working.set(Calendar.MILLISECOND, 0);
105        return working.getTime();
106}
107
108/**
109 * Return the end of a minute containing a specified time
110 * @param value Specified time
111 * @return End of minute
112 */
113public static Date endOfMinute(Date value)
114{
115        Calendar working = new GregorianCalendar();
116        working.setTime(value);
117        working.set(Calendar.SECOND, 59);
118        working.set(Calendar.MILLISECOND, 999);
119        return working.getTime();
120}
121
122/**
123 * Test driver for class.
124 * @param args Not used
125 */
126public static void main (String args[])
127{
128        System.out.println("Test driver for bradleyross.library.helpers.DateHelpers");
129        Date now = new Date();
130        java.text.SimpleDateFormat formatter =
131                new java.text.SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss.SSS");
132        System.out.println("Current time is " + formatter.format(now));
133        System.out.println("Start of month is " + formatter.format(startOfMonth(now)));
134        System.out.println("Start of day is " + formatter.format(startOfDay(now)));
135        System.out.println("End of day is " + formatter.format(endOfDay(now)));
136        System.out.println("Start of hour is " + formatter.format(startOfHour(now)));
137        System.out.println("End of hour is " + formatter.format(endOfHour(now)));
138        System.out.println("Start of minute is " + formatter.format(startOfMinute(now)));
139        System.out.println("End of minute is " + formatter.format(endOfMinute(now)));
140}
141}