1 50 51 package org.jfree.data.time; 52 53 import java.lang.reflect.Constructor ; 54 import java.util.Calendar ; 55 import java.util.Date ; 56 import java.util.TimeZone ; 57 58 import org.jfree.date.MonthConstants; 59 60 68 public abstract class RegularTimePeriod implements TimePeriod, Comparable , 69 MonthConstants { 70 71 81 public static RegularTimePeriod createInstance(Class c, Date millisecond, 82 TimeZone zone) { 83 RegularTimePeriod result = null; 84 try { 85 Constructor constructor = c.getDeclaredConstructor( 86 new Class [] {Date .class, TimeZone .class} 87 ); 88 result = (RegularTimePeriod) constructor.newInstance( 89 new Object [] {millisecond, zone} 90 ); 91 } 92 catch (Exception e) { 93 } 95 return result; 96 } 97 98 106 public static Class downsize(Class c) { 107 if (c.equals(Year.class)) { 108 return Quarter.class; 109 } 110 else if (c.equals(Quarter.class)) { 111 return Month.class; 112 } 113 else if (c.equals(Month.class)) { 114 return Day.class; 115 } 116 else if (c.equals(Day.class)) { 117 return Hour.class; 118 } 119 else if (c.equals(Hour.class)) { 120 return Minute.class; 121 } 122 else if (c.equals(Minute.class)) { 123 return Second.class; 124 } 125 else if (c.equals(Second.class)) { 126 return Millisecond.class; 127 } 128 else { 129 return Millisecond.class; 130 } 131 } 132 133 139 public abstract RegularTimePeriod previous(); 140 141 147 public abstract RegularTimePeriod next(); 148 149 154 public abstract long getSerialIndex(); 155 156 158 159 public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getDefault(); 160 161 162 public static final Calendar WORKING_CALENDAR 163 = Calendar.getInstance(DEFAULT_TIME_ZONE); 164 165 170 public Date getStart() { 171 return new Date (getFirstMillisecond()); 172 } 173 174 179 public Date getEnd() { 180 return new Date (getLastMillisecond()); 181 } 182 183 189 public long getFirstMillisecond() { 190 return getFirstMillisecond(DEFAULT_TIME_ZONE); 191 } 192 193 201 public long getFirstMillisecond(TimeZone zone) { 202 WORKING_CALENDAR.setTimeZone(zone); 203 return getFirstMillisecond(WORKING_CALENDAR); 204 } 205 206 214 public abstract long getFirstMillisecond(Calendar calendar); 215 216 222 public long getLastMillisecond() { 223 return getLastMillisecond(DEFAULT_TIME_ZONE); 224 } 225 226 234 public long getLastMillisecond(TimeZone zone) { 235 WORKING_CALENDAR.setTimeZone(zone); 236 return getLastMillisecond(WORKING_CALENDAR); 237 } 238 239 247 public abstract long getLastMillisecond(Calendar calendar); 248 249 255 public long getMiddleMillisecond() { 256 long m1 = getFirstMillisecond(); 257 long m2 = getLastMillisecond(); 258 return m1 + (m2 - m1) / 2; 259 } 260 261 269 public long getMiddleMillisecond(TimeZone zone) { 270 long m1 = getFirstMillisecond(zone); 271 long m2 = getLastMillisecond(zone); 272 return m1 + (m2 - m1) / 2; 273 } 274 275 283 public long getMiddleMillisecond(Calendar calendar) { 284 long m1 = getFirstMillisecond(calendar); 285 long m2 = getLastMillisecond(calendar); 286 return m1 + (m2 - m1) / 2; 287 } 288 289 294 public String toString() { 295 return String.valueOf(getStart()); 296 } 297 298 } 299 | Popular Tags |