1 23 package com.catcode.odf; 24 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 27 32 public class Duration 33 { 34 protected int years; 35 protected int months; 36 protected int days; 37 protected int hours; 38 protected int minutes; 39 protected double seconds; 40 protected boolean negative; 41 42 private static final Pattern durationPattern = 43 Pattern.compile("-?P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?" 44 + "(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:([\\d.]+)S)?)?"); 45 46 50 public Duration() 51 { 52 this( 0, 0, 0, 0, 0, 0.0 ); 53 } 54 55 64 public Duration( int years, int months, int days ) 65 { 66 this( years, months, days, 0, 0, 0.0 ); 67 } 68 69 80 public Duration( int years, int months, int days, 81 int hours, int minutes, double seconds ) 82 { 83 this.years = years; 84 this.months = months; 85 this.days = days; 86 this.hours = hours; 87 this.minutes = minutes; 88 this.seconds = seconds; 89 this.negative = false; 90 } 91 92 96 public int getYears() 97 { 98 return this.years; 99 } 100 101 107 public void setYears(int years) 108 { 109 this.years = years; 110 } 111 112 116 public int getMonths() 117 { 118 return this.months; 119 } 120 121 127 public void setMonths(int months) 128 { 129 this.months = months; 130 } 131 132 136 public int getDays() 137 { 138 return this.days; 139 } 140 141 147 public void setDays(int days) 148 { 149 this.days = days; 150 } 151 152 156 public int getHours() 157 { 158 return this.hours; 159 } 160 161 167 public void setHours(int hours) 168 { 169 this.hours = hours; 170 } 171 172 176 public int getMinutes() 177 { 178 return this.minutes; 179 } 180 181 187 public void setMinutes(int minutes) 188 { 189 this.minutes = minutes; 190 } 191 192 196 public double getSeconds() 197 { 198 return this.seconds; 199 } 200 201 207 public void setSeconds(double seconds) 208 { 209 this.seconds = seconds; 210 } 211 212 217 public boolean isNegative() 218 { 219 return this.negative; 220 } 221 222 228 public void setNegative( boolean negative ) 229 { 230 this.negative = negative; 231 } 232 233 242 public static Duration parseDuration( String str ) 243 { 244 String part; 245 Duration d = new Duration(); 246 Matcher m = durationPattern.matcher( str ); 247 if (m.find()) 248 { 249 d.negative = (str.startsWith("-")); 250 try 251 { 252 part = m.group(1); 253 d.years = (part != null ) ? Integer.parseInt( part ) : 0; 254 part = m.group(2); 255 d.months = (part != null ) ? Integer.parseInt( part ) : 0; 256 part = m.group(3); 257 d.days = (part != null ) ? Integer.parseInt( part ) : 0; 258 part = m.group(4); 259 d.hours = (part != null ) ? Integer.parseInt( part ) : 0; 260 part = m.group(5); 261 d.minutes = (part != null ) ? Integer.parseInt( part ) : 0; 262 part = m.group(6); 263 d.seconds = (part != null ) ? Double.parseDouble( part ) : 0.0; 264 } 265 catch (Exception e) 266 { 267 d = null; 268 } 269 } 270 else 271 { 272 d = null; 273 } 274 return d; 275 } 276 277 284 public String toString( ) 285 { 286 String result = (negative) ? "-P" : "P"; 287 288 if (years > 0 || months > 0 || days > 0) 289 { 290 if (years > 0) 291 { 292 result += years + "Y"; 293 } 294 if (months > 0) 295 { 296 result += months + "M"; 297 } 298 if (days > 0) 299 { 300 result += days + "D"; 301 } 302 } 303 else 304 { 305 result += "0D"; 306 } 307 if (hours > 0 || minutes > 0 || seconds > 0) 308 { 309 result += "T"; 310 if (hours > 0) 311 { 312 result += hours + "H"; 313 } 314 if (minutes > 0) 315 { 316 result += minutes + "M"; 317 } 318 if (seconds > 0) 319 { 320 result += seconds + "S"; 321 } 322 } 323 return result; 324 } 325 } 326 327 | Popular Tags |