1 24 25 package com.mckoi.util; 26 27 import java.math.BigDecimal ; 28 import java.util.Date ; 29 import java.text.StringCharacterIterator ; 30 31 40 41 public class TimeFrame { 42 43 private static final BigDecimal BD_ZERO = new BigDecimal (0); 44 45 48 public static int WEEKS = 1, 49 DAYS = 2, 50 HOURS = 3, 51 MINUTES = 4; 52 53 57 private BigDecimal period; 58 59 62 public TimeFrame(BigDecimal period) { 63 this.period = period; 64 } 65 66 69 public BigDecimal getPeriod() { 70 return period; 71 } 72 73 76 public boolean isNoTime() { 77 return period.equals(BD_ZERO); 78 } 79 80 84 public Date addToDate(Date date) { 85 return new Date (date.getTime() + period.longValue()); 86 } 87 88 94 public String format(int format_type) { 95 return format(format_type, true); 96 } 97 98 105 public String format(int format_type, boolean shorthand) { 106 if (period == null) { 107 return ""; 108 } 109 StringBuffer str = new StringBuffer (); 110 double val = period.longValue(); 111 if (format_type == WEEKS) { 112 GeneralFormatter.appendWeekType(str, val, shorthand); 113 } 114 else if (format_type == DAYS) { 115 GeneralFormatter.appendDayType(str, val, shorthand); 116 } 117 else if (format_type == HOURS) { 118 GeneralFormatter.appendHourType(str, val, shorthand); 119 } 120 else if (format_type == MINUTES) { 121 GeneralFormatter.appendMinuteType(str, val, shorthand); 122 } 123 return str.toString(); 124 } 125 126 135 public static TimeFrame parse(String str) throws java.text.ParseException { 136 if (str == null || str.equals("")) { 138 return null; 139 } 140 141 BigDecimal period = 142 GeneralParser.parseTimeMeasure(new StringCharacterIterator (str)); 143 return new TimeFrame(period); 144 } 145 146 149 public boolean equals(Object ob) { 150 TimeFrame tf = (TimeFrame) ob; 151 if (tf == null) { 152 return false; 153 } 154 return (this == tf || period.equals(tf.period)); 155 } 156 157 160 public String toString() { 161 return format(WEEKS); 162 } 163 164 } 165 | Popular Tags |