1 42 package org.jfree.chart.util; 43 44 import java.text.DateFormat ; 45 import java.text.DecimalFormat ; 46 import java.text.FieldPosition ; 47 import java.text.NumberFormat ; 48 import java.text.ParsePosition ; 49 import java.util.Calendar ; 50 import java.util.Date ; 51 import java.util.GregorianCalendar ; 52 53 59 public class RelativeDateFormat extends DateFormat { 60 61 62 private long baseMillis; 63 64 67 private boolean showZeroDays; 68 69 73 private NumberFormat dayFormatter; 74 75 78 private String daySuffix; 79 80 83 private String hourSuffix; 84 85 88 private String minuteSuffix; 89 90 93 private NumberFormat secondFormatter; 94 95 98 private String secondSuffix; 99 100 103 private static long MILLISECONDS_IN_ONE_HOUR = 60 * 60 * 1000L; 104 105 108 private static long MILLISECONDS_IN_ONE_DAY = 24 * MILLISECONDS_IN_ONE_HOUR; 109 110 113 public RelativeDateFormat() { 114 this(0L); 115 } 116 117 122 public RelativeDateFormat(Date time) { 123 this(time.getTime()); 124 } 125 126 131 public RelativeDateFormat(long baseMillis) { 132 super(); 133 this.baseMillis = baseMillis; 134 this.showZeroDays = false; 135 this.dayFormatter = NumberFormat.getInstance(); 136 this.daySuffix = "d"; 137 this.hourSuffix = "h"; 138 this.minuteSuffix = "m"; 139 this.secondFormatter = NumberFormat.getNumberInstance(); 140 this.secondFormatter.setMaximumFractionDigits(3); 141 this.secondFormatter.setMinimumFractionDigits(3); 142 this.secondSuffix = "s"; 143 } 144 145 153 public long getBaseMillis() { 154 return this.baseMillis; 155 } 156 157 166 public void setBaseMillis(long baseMillis) { 167 this.baseMillis = baseMillis; 168 } 169 170 178 public boolean getShowZeroDays() { 179 return this.showZeroDays; 180 } 181 182 190 public void setShowZeroDays(boolean show) { 191 this.showZeroDays = show; 192 } 193 194 201 public String getDaySuffix() { 202 return this.daySuffix; 203 } 204 205 212 public void setDaySuffix(String suffix) { 213 this.daySuffix = suffix; 214 } 215 216 223 public String getHourSuffix() { 224 return this.hourSuffix; 225 } 226 227 234 public void setHourSuffix(String suffix) { 235 this.hourSuffix = suffix; 236 } 237 238 245 public String getMinuteSuffix() { 246 return this.minuteSuffix; 247 } 248 249 256 public void setMinuteSuffix(String suffix) { 257 this.minuteSuffix = suffix; 258 } 259 260 267 public String getSecondSuffix() { 268 return this.secondSuffix; 269 } 270 271 278 public void setSecondSuffix(String suffix) { 279 this.secondSuffix = suffix; 280 } 281 282 287 public void setSecondFormatter(NumberFormat formatter) { 288 if (formatter == null) { 289 throw new IllegalArgumentException ("Null 'formatter' argument."); 290 } 291 this.secondFormatter = formatter; 292 } 293 294 304 public StringBuffer format(Date date, StringBuffer toAppendTo, 305 FieldPosition fieldPosition) { 306 long currentMillis = date.getTime(); 307 long elapsed = currentMillis - baseMillis; 308 309 long days = elapsed / MILLISECONDS_IN_ONE_DAY; 310 elapsed = elapsed - (days * MILLISECONDS_IN_ONE_DAY); 311 long hours = elapsed / MILLISECONDS_IN_ONE_HOUR; 312 elapsed = elapsed - (hours * MILLISECONDS_IN_ONE_HOUR); 313 long minutes = elapsed / 60000L; 314 elapsed = elapsed - (minutes * 60000L); 315 double seconds = elapsed / 1000.0; 316 if (days != 0 || this.showZeroDays) { 317 toAppendTo.append(this.dayFormatter.format(days) + getDaySuffix()); 318 } 319 toAppendTo.append(String.valueOf(hours) + getHourSuffix()); 320 toAppendTo.append(String.valueOf(minutes) + getMinuteSuffix()); 321 toAppendTo.append(this.secondFormatter.format(seconds) 322 + getSecondSuffix()); 323 return toAppendTo; 324 } 325 326 334 public Date parse(String source, ParsePosition pos) { 335 return null; 336 } 337 338 345 public boolean equals(Object obj) { 346 if (obj == this) { 347 return true; 348 } 349 if (!(obj instanceof RelativeDateFormat)) { 350 return false; 351 } 352 if (!super.equals(obj)) { 353 return false; 354 } 355 RelativeDateFormat that = (RelativeDateFormat) obj; 356 if (this.baseMillis != that.baseMillis) { 357 return false; 358 } 359 return true; 360 } 361 362 367 public static void main(String [] args) { 368 GregorianCalendar c0 = new GregorianCalendar (2006, 10, 1, 0, 0, 0); 369 GregorianCalendar c1 = new GregorianCalendar (2006, 10, 1, 11, 37, 43); 370 c1.set(Calendar.MILLISECOND, 123); 371 372 System.out.println("Default: "); 373 RelativeDateFormat rdf = new RelativeDateFormat(c0.getTimeInMillis()); 374 System.out.println(rdf.format(c1.getTime())); 375 System.out.println(); 376 377 System.out.println("Hide milliseconds: "); 378 rdf.setSecondFormatter(new DecimalFormat ("0")); 379 System.out.println(rdf.format(c1.getTime())); 380 System.out.println(); 381 382 System.out.println("Show zero day output: "); 383 rdf.setShowZeroDays(true); 384 System.out.println(rdf.format(c1.getTime())); 385 System.out.println(); 386 387 System.out.println("Alternative suffixes: "); 388 rdf.setShowZeroDays(false); 389 rdf.setDaySuffix(":"); 390 rdf.setHourSuffix(":"); 391 rdf.setMinuteSuffix(":"); 392 rdf.setSecondSuffix(""); 393 System.out.println(rdf.format(c1.getTime())); 394 System.out.println(); 395 } 396 } 397 | Popular Tags |