Changeset 17104 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2020-10-07T20:42:43+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java
r15869 r17104 10 10 import java.time.ZonedDateTime; 11 11 import java.time.format.DateTimeFormatter; 12 import java.time.format.DateTimeParseException; 12 13 import java.util.Date; 13 14 import java.util.Locale; … … 15 16 import java.util.concurrent.TimeUnit; 16 17 17 import javax.xml.datatype.DatatypeConfigurationException;18 import javax.xml.datatype.DatatypeFactory;19 20 18 import org.openstreetmap.josm.data.preferences.BooleanProperty; 21 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 22 import org.openstreetmap.josm.tools.Logging;23 20 import org.openstreetmap.josm.tools.UncheckedParseException; 24 21 … … 44 41 public static final BooleanProperty PROP_ISO_DATES = new BooleanProperty("iso.dates", false); 45 42 46 private static final DatatypeFactory XML_DATE;47 48 static {49 DatatypeFactory fact = null;50 try {51 fact = DatatypeFactory.newInstance();52 } catch (DatatypeConfigurationException e) {53 Logging.error(e);54 }55 XML_DATE = fact;56 }57 58 43 /** 59 44 * Constructs a new {@code DateUtils}. … … 70 55 * @throws DateTimeException if the value of any field is out of range, or if the day-of-month is invalid for the month-year 71 56 */ 72 public static synchronizedDate fromString(String str) {57 public static Date fromString(String str) { 73 58 return new Date(tsFromString(str)); 74 59 } … … 81 66 * @throws DateTimeException if the value of any field is out of range, or if the day-of-month is invalid for the month-year 82 67 */ 83 public static synchronizedlong tsFromString(String str) {68 public static long tsFromString(String str) { 84 69 // "2007-07-25T09:26:24{Z|{+|-}01[:00]}" 85 if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") || 70 if (checkLayout(str, "xxxx-xx-xx")) { 71 final ZonedDateTime local = ZonedDateTime.of( 72 parsePart4(str, 0), 73 parsePart2(str, 5), 74 parsePart2(str, 8), 75 0, 0, 0, 0, ZoneOffset.UTC); 76 return local.toInstant().toEpochMilli(); 77 } else if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") || 86 78 checkLayout(str, "xxxx-xx-xxTxx:xx:xx") || 87 79 checkLayout(str, "xxxx:xx:xx xx:xx:xx") || … … 136 128 137 129 try { 138 return XML_DATE.newXMLGregorianCalendar(str).toGregorianCalendar().getTimeInMillis(); 139 } catch (IllegalArgumentException ex) { 130 // slow path for fractional seconds different from millisecond precision 131 return Instant.parse(str).toEpochMilli(); 132 } catch (IllegalArgumentException | DateTimeParseException ex) { 140 133 throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex); 141 134 } … … 158 151 * @since 14434 159 152 */ 160 public static synchronizedString fromTimestampInMillis(long timestamp) {153 public static String fromTimestampInMillis(long timestamp) { 161 154 final ZonedDateTime temporal = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC); 162 155 return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(temporal); … … 168 161 * @return The formatted date 169 162 */ 170 public static synchronizedString fromTimestamp(int timestamp) {163 public static String fromTimestamp(int timestamp) { 171 164 return fromTimestamp(Integer.toUnsignedLong(timestamp)); 172 165 } … … 177 170 * @return The formatted date 178 171 */ 179 public static synchronizedString fromDate(Date date) {172 public static String fromDate(Date date) { 180 173 final ZonedDateTime temporal = date.toInstant().atZone(ZoneOffset.UTC); 181 174 return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(temporal);
Note:
See TracChangeset
for help on using the changeset viewer.