- Timestamp:
- 2016-07-03T22:16:57+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java
r10483 r10513 35 35 public static final TimeZone UTC = TimeZone.getTimeZone("UTC"); 36 36 37 protected DateUtils() {38 // Hide default constructor for utils classes39 }40 41 37 /** 42 38 * Property to enable display of ISO dates globally. … … 52 48 */ 53 49 private static final GregorianCalendar calendar = new GregorianCalendar(UTC); 50 /** 51 * A shared instance to convert local times. The time zone should be set before every conversion. 52 */ 54 53 private static final GregorianCalendar calendarLocale = new GregorianCalendar(TimeZone.getDefault()); 55 54 private static final DatatypeFactory XML_DATE; … … 68 67 } 69 68 69 protected DateUtils() { 70 // Hide default constructor for utils classes 71 } 72 70 73 /** 71 74 * Parses XML date quickly, regardless of current locale. … … 74 77 * @throws UncheckedParseException if the date does not match any of the supported date formats 75 78 */ 76 public static synchronized Date fromString(String str) throws UncheckedParseException{79 public static synchronized Date fromString(String str) { 77 80 return new Date(tsFromString(str)); 78 81 } … … 84 87 * @throws UncheckedParseException if the date does not match any of the supported date formats 85 88 */ 86 public static synchronized long tsFromString(String str) throws UncheckedParseException{89 public static synchronized long tsFromString(String str) { 87 90 // "2007-07-25T09:26:24{Z|{+|-}01[:00]}" 88 91 if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") || … … 94 97 checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") || 95 98 checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) { 96 final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx") ? calendarLocale : calendar; // consider EXIF date in default timezone 99 final Calendar c; // consider EXIF date in default timezone 100 if (checkLayout(str, "xxxx:xx:xx xx:xx:xx")) { 101 c = getLocalCalendar(); 102 } else { 103 c = calendar; 104 } 97 105 c.set( 98 106 parsePart4(str, 0), … … 116 124 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") || 117 125 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) { 118 final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? calendarLocale : calendar; // consider EXIF date in default timezone 126 // consider EXIF date in default timezone 127 final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? getLocalCalendar() : calendar; 119 128 c.set( 120 129 parsePart4(str, 0), … … 134 143 // example date format "18-AUG-08 13:33:03" 135 144 SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss"); 136 f.setTimeZone(calendarLocale.getTimeZone());137 145 Date d = f.parse(str, new ParsePosition(0)); 138 146 if (d != null) … … 145 153 throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex); 146 154 } 155 } 156 157 private static Calendar getLocalCalendar() { 158 final Calendar c = calendarLocale; 159 c.setTimeZone(TimeZone.getDefault()); 160 return c; 147 161 } 148 162 … … 316 330 return getDateTimeFormat(dateStyle, timeStyle).format(datetime); 317 331 } 318 319 /**320 * Allows to override the timezone for unit tests.321 * @param zone the timezone to use322 */323 protected static synchronized void setTimeZone(TimeZone zone) {324 calendarLocale.setTimeZone(zone);325 }326 332 }
Note:
See TracChangeset
for help on using the changeset viewer.