1 16 package org.joda.time.convert; 17 18 import org.joda.time.Chronology; 19 import org.joda.time.DateTime; 20 import org.joda.time.Period; 21 import org.joda.time.ReadWritableInterval; 22 import org.joda.time.ReadWritablePeriod; 23 import org.joda.time.ReadablePartial; 24 import org.joda.time.field.FieldUtils; 25 import org.joda.time.format.DateTimeFormatter; 26 import org.joda.time.format.ISODateTimeFormat; 27 import org.joda.time.format.ISOPeriodFormat; 28 import org.joda.time.format.PeriodFormatter; 29 30 38 class StringConverter extends AbstractConverter 39 implements InstantConverter, PartialConverter, DurationConverter, PeriodConverter, IntervalConverter { 40 41 44 static final StringConverter INSTANCE = new StringConverter(); 45 46 49 protected StringConverter() { 50 super(); 51 } 52 53 62 public long getInstantMillis(Object object, Chronology chrono) { 63 String str = (String ) object; 64 DateTimeFormatter p = ISODateTimeFormat.dateTimeParser(); 65 return p.withChronology(chrono).parseMillis(str); 66 } 67 68 83 public int[] getPartialValues(ReadablePartial fieldSource, Object object, Chronology chrono, DateTimeFormatter parser) { 84 if (parser.getZone() != null) { 85 chrono = chrono.withZone(parser.getZone()); 86 } 87 long millis = parser.withChronology(chrono).parseMillis((String ) object); 88 return chrono.get(fieldSource, millis); 89 } 90 91 99 public long getDurationMillis(Object object) { 100 String original = (String ) object; 103 String str = original; 104 int len = str.length(); 105 if (len >= 4 && 106 (str.charAt(0) == 'P' || str.charAt(0) == 'p') && 107 (str.charAt(1) == 'T' || str.charAt(1) == 't') && 108 (str.charAt(len - 1) == 'S' || str.charAt(len - 1) == 's')) { 109 } else { 111 throw new IllegalArgumentException ("Invalid format: \"" + original + '"'); 112 } 113 str = str.substring(2, len - 1); 114 int dot = -1; 115 for (int i = 0; i < str.length(); i++) { 116 if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') || 117 (i == 0 && str.charAt(0) == '-')) { 118 } else if (i > 0 && str.charAt(i) == '.' && dot == -1) { 120 dot = i; 122 } else { 123 throw new IllegalArgumentException ("Invalid format: \"" + original + '"'); 124 } 125 } 126 long millis = 0, seconds = 0; 127 if (dot > 0) { 128 seconds = Long.parseLong(str.substring(0, dot)); 129 str = str.substring(dot + 1); 130 if (str.length() != 3) { 131 str = (str + "000").substring(0, 3); 132 } 133 millis = Integer.parseInt(str); 134 } else { 135 seconds = Long.parseLong(str); 136 } 137 if (seconds < 0) { 138 return FieldUtils.safeAdd(FieldUtils.safeMultiply(seconds, 1000), -millis); 139 } else { 140 return FieldUtils.safeAdd(FieldUtils.safeMultiply(seconds, 1000), millis); 141 } 142 } 143 144 155 public void setInto(ReadWritablePeriod period, Object object, Chronology chrono) { 156 String str = (String ) object; 157 PeriodFormatter parser = ISOPeriodFormat.standard(); 158 period.clear(); 159 int pos = parser.parseInto(period, str, 0); 160 if (pos < str.length()) { 161 if (pos < 0) { 162 parser.withParseType(period.getPeriodType()).parseMutablePeriod(str); 164 } 165 throw new IllegalArgumentException ("Invalid format: \"" + str + '"'); 166 } 167 } 168 169 177 public void setInto(ReadWritableInterval writableInterval, Object object, Chronology chrono) { 178 String str = (String ) object; 179 180 int separator = str.indexOf('/'); 181 if (separator < 0) { 182 throw new IllegalArgumentException ("Format requires a '/' separator: " + str); 183 } 184 185 String leftStr = str.substring(0, separator); 186 if (leftStr.length() <= 0) { 187 throw new IllegalArgumentException ("Format invalid: " + str); 188 } 189 String rightStr = str.substring(separator + 1); 190 if (rightStr.length() <= 0) { 191 throw new IllegalArgumentException ("Format invalid: " + str); 192 } 193 194 DateTimeFormatter dateTimeParser = ISODateTimeFormat.dateTimeParser(); 195 dateTimeParser = dateTimeParser.withChronology(chrono); 196 PeriodFormatter periodParser = ISOPeriodFormat.standard(); 197 long startInstant = 0, endInstant = 0; 198 Period period = null; 199 Chronology parsedChrono = null; 200 201 char c = leftStr.charAt(0); 203 if (c == 'P' || c == 'p') { 204 period = periodParser.withParseType(getPeriodType(leftStr)).parsePeriod(leftStr); 205 } else { 206 DateTime start = dateTimeParser.parseDateTime(leftStr); 207 startInstant = start.getMillis(); 208 parsedChrono = start.getChronology(); 209 } 210 211 c = rightStr.charAt(0); 213 if (c == 'P' || c == 'p') { 214 if (period != null) { 215 throw new IllegalArgumentException ("Interval composed of two durations: " + str); 216 } 217 period = periodParser.withParseType(getPeriodType(rightStr)).parsePeriod(rightStr); 218 chrono = (chrono != null ? chrono : parsedChrono); 219 endInstant = chrono.add(period, startInstant, 1); 220 } else { 221 DateTime end = dateTimeParser.parseDateTime(rightStr); 222 endInstant = end.getMillis(); 223 parsedChrono = (parsedChrono != null ? parsedChrono : end.getChronology()); 224 chrono = (chrono != null ? chrono : parsedChrono); 225 if (period != null) { 226 startInstant = chrono.add(period, endInstant, -1); 227 } 228 } 229 230 writableInterval.setInterval(startInstant, endInstant); 231 writableInterval.setChronology(chrono); 232 } 233 234 240 public Class getSupportedType() { 241 return String .class; 242 } 243 244 } 245 | Popular Tags |