Changeset 17104 in josm for trunk/src/org


Ignore:
Timestamp:
2020-10-07T20:42:43+02:00 (4 years ago)
Author:
simon04
Message:

fix #19704 - DateUtils: sychronization is no longer needed thanks to JSR 310

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r15869 r17104  
    1010import java.time.ZonedDateTime;
    1111import java.time.format.DateTimeFormatter;
     12import java.time.format.DateTimeParseException;
    1213import java.util.Date;
    1314import java.util.Locale;
     
    1516import java.util.concurrent.TimeUnit;
    1617
    17 import javax.xml.datatype.DatatypeConfigurationException;
    18 import javax.xml.datatype.DatatypeFactory;
    19 
    2018import org.openstreetmap.josm.data.preferences.BooleanProperty;
    2119import org.openstreetmap.josm.tools.CheckParameterUtil;
    22 import org.openstreetmap.josm.tools.Logging;
    2320import org.openstreetmap.josm.tools.UncheckedParseException;
    2421
     
    4441    public static final BooleanProperty PROP_ISO_DATES = new BooleanProperty("iso.dates", false);
    4542
    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 
    5843    /**
    5944     * Constructs a new {@code DateUtils}.
     
    7055     * @throws DateTimeException if the value of any field is out of range, or if the day-of-month is invalid for the month-year
    7156     */
    72     public static synchronized Date fromString(String str) {
     57    public static Date fromString(String str) {
    7358        return new Date(tsFromString(str));
    7459    }
     
    8166     * @throws DateTimeException if the value of any field is out of range, or if the day-of-month is invalid for the month-year
    8267     */
    83     public static synchronized long tsFromString(String str) {
     68    public static long tsFromString(String str) {
    8469        // "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") ||
    8678                checkLayout(str, "xxxx-xx-xxTxx:xx:xx") ||
    8779                checkLayout(str, "xxxx:xx:xx xx:xx:xx") ||
     
    136128
    137129        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) {
    140133            throw new UncheckedParseException("The date string (" + str + ") could not be parsed.", ex);
    141134        }
     
    158151     * @since 14434
    159152     */
    160     public static synchronized String fromTimestampInMillis(long timestamp) {
     153    public static String fromTimestampInMillis(long timestamp) {
    161154        final ZonedDateTime temporal = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.UTC);
    162155        return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(temporal);
     
    168161     * @return The formatted date
    169162     */
    170     public static synchronized String fromTimestamp(int timestamp) {
     163    public static String fromTimestamp(int timestamp) {
    171164        return fromTimestamp(Integer.toUnsignedLong(timestamp));
    172165    }
     
    177170     * @return The formatted date
    178171     */
    179     public static synchronized String fromDate(Date date) {
     172    public static String fromDate(Date date) {
    180173        final ZonedDateTime temporal = date.toInstant().atZone(ZoneOffset.UTC);
    181174        return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(temporal);
Note: See TracChangeset for help on using the changeset viewer.