001package bradleyross.library.helpers; 002import java.util.Date; 003import java.util.TimeZone; 004import java.util.Calendar; 005import java.util.GregorianCalendar; 006/** 007 * Classes for manipulating calendar dates. 008 * @author Bradley Ross 009 * 010 */ 011public class CalendarHelpers 012{ 013 protected Calendar refCalendar; 014 /** 015 * Controls amount of intermediate output to aid 016 * in software development and diagnostices. 017 * <p>A more positive value indicates that more 018 * output is to be generated. A more negative 019 * value indicates that less output is to be 020 * generated.</p> 021 */ 022 protected int debugLevel = 0; 023 /** 024 * Getter for debugLevel 025 * @see #debugLevel 026 * @return Value of debugLevel 027 */ 028 public int getDebugLevel() 029 { 030 return debugLevel; 031 } 032 /** 033 * Setter for debugLevel 034 * @see #debugLevel 035 * @param value Value for debugLevel 036 */ 037 public void setDebugLevel(int value) 038 { 039 debugLevel = value; 040 } 041 public CalendarHelpers() 042 { 043 refCalendar = new GregorianCalendar(); 044 } 045 public CalendarHelpers(TimeZone value) 046 { 047 refCalendar = new GregorianCalendar(value); 048 } 049 public CalendarHelpers(Calendar value) 050 { 051 refCalendar = (Calendar) value.clone(); 052 } 053 /** 054 * Returns number of milliseconds since start of day. 055 * @param value Date for which calculation is to be run 056 * @return Number of milliseconds 057 */ 058 public long getTimeOfDay(Date value) 059 { 060 long working; 061 Calendar local = (Calendar) refCalendar.clone(); 062 local.setTime(value); 063 working = (long) local.get(Calendar.MILLISECOND) + 064 (long) (local.get(Calendar.SECOND)*1000) + 065 (long) (local.get(Calendar.MINUTE)*60*1000) + 066 (long) (local.get(Calendar.HOUR_OF_DAY)*60*1000*60); 067 return working; 068 } 069 public Date getStartOfDay(Date value) 070 { 071 Calendar local = (Calendar) refCalendar.clone(); 072 local.setTime(value); 073 local.set(Calendar.MILLISECOND, 0); 074 local.set(Calendar.SECOND, 0); 075 local.set(Calendar.MINUTE, 0); 076 local.set(Calendar.HOUR_OF_DAY, 0); 077 return local.getTime(); 078 } 079 public Date getNextDay(Date value) 080 { 081 Calendar local = (Calendar) refCalendar.clone(); 082 local.setTime(getStartOfDay(value)); 083 local.add(Calendar.DAY_OF_MONTH, 1); 084 return local.getTime(); 085 } 086 /** 087 * This was to get around a bug in some data feeds. 088 * <p>The problem was that the source program was incorrectly assigning 089 * am and pm for some times. It appears that this problem has now been corrected.</p> 090 * @param value Date as provided by program 091 * @return Corrected date 092 * @deprecated 093 */ 094 public Date specialFix(Date value) 095 { 096 Calendar local = (Calendar) refCalendar.clone(); 097 local.setTime(value); 098 int hour = local.get(Calendar.HOUR); 099 if (hour == 0) 100 { 101 local.set(Calendar.HOUR, 12); 102 } 103 else if (hour == 12) 104 { 105 local.set(Calendar.HOUR, 0); 106 } 107 return local.getTime(); 108 } 109 /** 110 * @param args 111 */ 112 public static void main(String[] args) 113 { 114 System.out.println("Starting program at " + new Date().toString()); 115 116 } 117 118}