| 1 |
pjm2 |
1.1 |
//---PACKAGE DECLARATION--- |
| 2 |
|
|
|
| 3 |
|
|
//---IMPORTS--- |
| 4 |
|
|
import java.util.*; |
| 5 |
|
|
import java.text.*; |
| 6 |
|
|
|
| 7 |
|
|
|
| 8 |
|
|
/** |
| 9 |
|
|
* Provides easy to use date functions. |
| 10 |
|
|
* |
| 11 |
|
|
* @author $Author: pjm2 $ |
| 12 |
|
|
* @version $Id: DateUtils.java,v 1.9 2001/01/11 19:03:04 pjm2 Exp $ |
| 13 |
|
|
*/ |
| 14 |
|
|
public class DateUtils { |
| 15 |
|
|
|
| 16 |
|
|
//---FINAL ATTRIBUTES--- |
| 17 |
|
|
|
| 18 |
|
|
/** |
| 19 |
|
|
* The current CVS revision of this class |
| 20 |
|
|
*/ |
| 21 |
|
|
public final String REVISION = "$Revision: 1.9 $"; |
| 22 |
|
|
|
| 23 |
|
|
public static final long secsPerMonth = 30*24*60*60; |
| 24 |
|
|
public static final long secsPerWeek = 7*24*60*60; |
| 25 |
|
|
public static final long secsPerDay = 24*60*60; |
| 26 |
|
|
public static final long secsPerHour = 60*60; |
| 27 |
|
|
|
| 28 |
|
|
//---STATIC METHODS--- |
| 29 |
|
|
|
| 30 |
|
|
/** |
| 31 |
|
|
* Return the number of seconds between the epoch and midnight earlier today. |
| 32 |
|
|
*/ |
| 33 |
|
|
public static long startOfToday(){ |
| 34 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd"); |
| 35 |
|
|
Date midnight = new Date(); |
| 36 |
|
|
String then = formatter.format(midnight); |
| 37 |
|
|
|
| 38 |
|
|
ParsePosition pos = new ParsePosition(0); |
| 39 |
|
|
midnight = formatter.parse(then, pos); |
| 40 |
|
|
|
| 41 |
|
|
return midnight.getTime()/1000; |
| 42 |
|
|
} |
| 43 |
|
|
|
| 44 |
|
|
/** |
| 45 |
|
|
* Return the number of seconds between the epoch and now. |
| 46 |
|
|
*/ |
| 47 |
|
|
public static long now(){ |
| 48 |
|
|
Date now = new Date(); |
| 49 |
|
|
return now.getTime()/1000; |
| 50 |
|
|
} |
| 51 |
|
|
|
| 52 |
|
|
/** |
| 53 |
|
|
* Accept a long representing the number of seconds since the epoch. |
| 54 |
|
|
* An integer specifies the number of months to subtract. |
| 55 |
|
|
* Returns a long representing the number of seconds since the epoch. |
| 56 |
|
|
*/ |
| 57 |
|
|
public static long subtractMonths(long before, int months){ |
| 58 |
|
|
return before - months*secsPerMonth; |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
|
|
/** |
| 62 |
|
|
* Accept a long representing the number of seconds since the epoch. |
| 63 |
|
|
* An integer specifies the number of weeks to subtract. |
| 64 |
|
|
* Returns a long representing the number of seconds since the epoch. |
| 65 |
|
|
*/ |
| 66 |
|
|
public static long subtractWeeks(long before, int weeks){ |
| 67 |
|
|
return before - weeks*secsPerWeek; |
| 68 |
|
|
} |
| 69 |
|
|
|
| 70 |
|
|
/** |
| 71 |
|
|
* Accept a long representing the number of seconds since the epoch. |
| 72 |
|
|
* An integer specifies the number of days to subtract. |
| 73 |
|
|
* Returns a long representing the number of seconds since the epoch. |
| 74 |
|
|
*/ |
| 75 |
|
|
public static long subtractDays(long before, int days){ |
| 76 |
|
|
return before - days*secsPerDay; |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
/** |
| 80 |
|
|
* Accept a long representing the number of seconds since the epoch. |
| 81 |
|
|
* An integer specifies the number of hours to subtract. |
| 82 |
|
|
* Returns a long representing the number of seconds since the epoch. |
| 83 |
|
|
*/ |
| 84 |
|
|
public static long subtractHours(long before, int hours){ |
| 85 |
|
|
return before - hours*secsPerHour; |
| 86 |
|
|
} |
| 87 |
|
|
|
| 88 |
|
|
/** |
| 89 |
|
|
* Return a nice String representation of the date. |
| 90 |
|
|
*/ |
| 91 |
|
|
public static String dateString(long t){ |
| 92 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 93 |
|
|
Date conv = new Date(t*1000); |
| 94 |
|
|
return formatter.format(conv); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
/** |
| 98 |
|
|
* Return a nice short String representation of the date. |
| 99 |
|
|
*/ |
| 100 |
|
|
public static String shortDateString(long t){ |
| 101 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); |
| 102 |
|
|
Date conv = new Date(t*1000); |
| 103 |
|
|
return formatter.format(conv); |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
/** |
| 107 |
|
|
* Return a String representation of the hour. |
| 108 |
|
|
*/ |
| 109 |
|
|
public static String hourString(long t){ |
| 110 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("H"); |
| 111 |
|
|
Date conv = new Date(t*1000); |
| 112 |
|
|
return formatter.format(conv); |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
|
/** |
| 116 |
|
|
* Return a String representation of the day name (short format). |
| 117 |
|
|
*/ |
| 118 |
|
|
public static String dayName(long t){ |
| 119 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("EEE"); |
| 120 |
|
|
Date conv = new Date(t*1000); |
| 121 |
|
|
return formatter.format(conv); |
| 122 |
|
|
} |
| 123 |
|
|
|
| 124 |
|
|
/** |
| 125 |
|
|
* Return a String representation of the time (short format). |
| 126 |
|
|
*/ |
| 127 |
|
|
public static String timeString(long t){ |
| 128 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm"); |
| 129 |
|
|
Date conv = new Date(t*1000); |
| 130 |
|
|
return formatter.format(conv); |
| 131 |
|
|
} |
| 132 |
|
|
|
| 133 |
|
|
|
| 134 |
|
|
//---CONSTRUCTORS--- |
| 135 |
|
|
|
| 136 |
|
|
//---PUBLIC METHODS--- |
| 137 |
|
|
|
| 138 |
|
|
/** |
| 139 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 140 |
|
|
* method to provide clean logging (every class should have this). |
| 141 |
|
|
* |
| 142 |
|
|
* @return the name of this class and its CVS revision |
| 143 |
|
|
*/ |
| 144 |
|
|
public String toString() { |
| 145 |
|
|
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
| 146 |
|
|
} |
| 147 |
|
|
|
| 148 |
|
|
//---PRIVATE METHODS--- |
| 149 |
|
|
|
| 150 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
| 151 |
|
|
|
| 152 |
|
|
//---ATTRIBUTES--- |
| 153 |
|
|
|
| 154 |
|
|
//---STATIC ATTRIBUTES--- |
| 155 |
|
|
|
| 156 |
|
|
} |