001 package edu.nrao.sss.util; 002 003 import java.text.DateFormat; 004 import java.text.SimpleDateFormat; 005 import java.util.Calendar; 006 import java.util.Date; 007 import java.util.TimeZone; 008 009 /** 010 * A helper class for working with {@link java.util.Date dates}. 011 * <p> 012 * <b>Version Info:</b> 013 * <table style="margin-left:2em"> 014 * <tr><td>$Revision$</td></tr> 015 * <tr><td>$Date$</td></tr> 016 * <tr><td>$Author$ (last person to modify)</td></tr> 017 * </table></p> 018 * 019 * @author David M. Harland 020 * @since 2007-09-19 021 */ 022 public class DateUtility 023 { 024 private static final Calendar CALENDAR = Calendar.getInstance(); 025 private static final Date LOW_DATE; 026 private static final Date HIGH_DATE; 027 028 static 029 { 030 CALENDAR.clear(); 031 CALENDAR.setTimeZone(TimeZone.getTimeZone("GMT")); 032 033 //Sept 14, 1752 (adoption of Gregorian by England & colonies) 034 CALENDAR.set(1752, 8, 14, 0, 0, 0); 035 LOW_DATE = CALENDAR.getTime(); 036 037 //Dec 31, 9999 038 CALENDAR.set(9999, 11, 31, 0, 0, 0); 039 HIGH_DATE = CALENDAR.getTime(); 040 } 041 042 /** 043 * Creates and returns a very low, or early, date. 044 * <p> 045 * This date is prior to any 046 * date that would reasonably be used in NRAO's SSS software. 047 * For the curious, this date is currently set to 048 * September 14, 1752, but no code should rely on this. 049 * (This is the first date that England and her colonies 050 * used the Gregorian Calendar.)</p> 051 */ 052 public static Date createEarlyDate() 053 { 054 //Dates are mutable, so we can't pass out the actual variable 055 return (Date)LOW_DATE.clone(); 056 } 057 058 /** 059 * Creates and returns a very high, or late, date. 060 * <p> 061 * This date is after any 062 * date that would reasonably be used in NRAO's SSS software. 063 * For the curious, this date is currently set to 064 * December 31, 9999, but no code should rely on this.</p> 065 */ 066 public static Date createLateDate() 067 { 068 //Dates are mutable, so we can't pass out the actual variable 069 return (Date)HIGH_DATE.clone(); 070 } 071 072 /** 073 * Returns <i>true</i> if this utility's 074 * {@link #createEarlyDate() earlyDate} is equal to 075 * {@code yourDate}. 076 * <p> 077 * Using this method is more efficient that calling 078 * <tt>DateUtility.createLateDate().equals(yourDate)</tt> 079 * because this method creates no new objects.</p> 080 * 081 * @param yourDate the date to be tested. 082 * 083 * @return <i>true</i> if your date is the early date. 084 */ 085 public static boolean earlyDateEquals(Date yourDate) 086 { 087 return LOW_DATE.equals(yourDate); 088 } 089 090 /** 091 * Returns <i>true</i> if this utility's 092 * {@link #createEarlyDate() earlyDate} is equal to 093 * {@code yourDate}. 094 * <p> 095 * Using this method is more efficient that calling 096 * <tt>DateUtility.createLateDate().equals(yourDate)</tt> 097 * because this method creates no new objects.</p> 098 * 099 * @param yourDate the date to be tested. 100 * 101 * @return <i>true</i> if your date is the early date. 102 */ 103 public static boolean lateDateEquals(Date yourDate) 104 { 105 return HIGH_DATE.equals(yourDate); 106 } 107 108 /** 109 * Creates and returns a date formatter than parses dates from, and 110 * writes dates to, 111 * <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> text. 112 * 113 * @return 114 * a date formatter built around the ISO 8601 specification. 115 */ 116 public static DateFormat makeIso8601Formatter() 117 { 118 return new SimpleDateFormat(FormatString.ISO8601_DATE_TIME_TIMEZONE); 119 } 120 }