Changeset 17840 in josm for trunk


Ignore:
Timestamp:
2021-05-01T12:37:44+02:00 (3 years ago)
Author:
simon04
Message:

see #14176 - Migrate ExceptionUtil to Instant

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/ChangesetClosedException.java

    r13207 r17840  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.text.ParseException;
    7 import java.util.Date;
     6import java.time.Instant;
    87import java.util.regex.Matcher;
    98import java.util.regex.Pattern;
    109
    1110import org.openstreetmap.josm.tools.Logging;
     11import org.openstreetmap.josm.tools.UncheckedParseException;
    1212import org.openstreetmap.josm.tools.date.DateUtils;
    1313
     
    6666    private long changesetId;
    6767    /** the date on which the changeset was closed */
    68     private Date closedOn;
     68    private Instant closedOn;
    6969    /** the source */
    7070    private Source source;
     
    7676            changesetId = Long.parseLong(m.group(1));
    7777            try {
    78                 closedOn = DateUtils.newOsmApiDateTimeFormat().parse(m.group(2));
    79             } catch (ParseException ex) {
     78                closedOn = DateUtils.parseInstant(m.group(2));
     79            } catch (UncheckedParseException ex) {
    8080                Logging.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
    8181                Logging.error(ex);
     
    129129     * @param source the source for the exception
    130130     */
    131     public ChangesetClosedException(long changesetId, Date closedOn, Source source) {
     131    public ChangesetClosedException(long changesetId, Instant closedOn, Source source) {
    132132        super("");
    133133        this.source = source == null ? Source.UNSPECIFIED : source;
    134134        this.changesetId = changesetId;
    135         this.closedOn = DateUtils.cloneDate(closedOn);
     135        this.closedOn = closedOn;
    136136    }
    137137
     
    150150     * @return the date the changeset was closed. May be null if the date isn't known.
    151151     */
    152     public Date getClosedOn() {
    153         return DateUtils.cloneDate(closedOn);
     152    public Instant getClosedOn() {
     153        return closedOn;
    154154    }
    155155
  • trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java

    r16643 r17840  
    1111import java.net.URL;
    1212import java.net.UnknownHostException;
    13 import java.text.DateFormat;
    14 import java.text.ParseException;
     13import java.time.Instant;
     14import java.time.ZoneId;
     15import java.time.format.FormatStyle;
    1516import java.util.Arrays;
    1617import java.util.Collection;
    17 import java.util.Date;
    1818import java.util.List;
    1919import java.util.Objects;
     
    398398            if (m.matches()) {
    399399                long changesetId = Long.parseLong(m.group(1));
    400                 Date closeDate = null;
     400                Instant closeDate = null;
    401401                try {
    402                     closeDate = DateUtils.newOsmApiDateTimeFormat().parse(m.group(2));
    403                 } catch (ParseException ex) {
     402                    closeDate = DateUtils.parseInstant(m.group(2));
     403                } catch (UncheckedParseException ex) {
    404404                    Logging.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
    405405                    Logging.error(ex);
     
    415415                            +" because it has already been closed on {1}.",
    416416                            changesetId,
    417                             DateUtils.formatDateTime(closeDate, DateFormat.DEFAULT, DateFormat.DEFAULT)
     417                            formatClosedOn(closeDate)
    418418                    );
    419419                }
     
    445445                +"because it has already been closed on {1}.",
    446446                e.getChangesetId(),
    447                 e.getClosedOn() == null ? "?" : DateUtils.formatDateTime(e.getClosedOn(), DateFormat.DEFAULT, DateFormat.DEFAULT)
     447                e.getClosedOn() == null ? "?" : formatClosedOn(e.getClosedOn())
    448448        );
     449    }
     450
     451    private static String formatClosedOn(Instant closedOn) {
     452        return DateUtils.getDateTimeFormatter(FormatStyle.SHORT, FormatStyle.SHORT).format(closedOn.atZone(ZoneId.systemDefault()));
    449453    }
    450454
  • trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java

    r17839 r17840  
    1111import java.time.ZonedDateTime;
    1212import java.time.format.DateTimeFormatter;
     13import java.time.format.DateTimeFormatterBuilder;
    1314import java.time.format.DateTimeParseException;
    1415import java.time.format.FormatStyle;
     
    7677     * @param str the date string
    7778     * @return the parsed instant
     79     * @throws UncheckedParseException if the date does not match any of the supported date formats
    7880     */
    7981    public static Instant parseInstant(String str) {
     
    249251
    250252    /**
    251      * Returns a new {@code SimpleDateFormat} for date and time, according to format used in OSM API errors.
    252      * @return a new date format, for date and time, to use for OSM API error handling.
    253      * @since 7299
    254      */
    255     public static SimpleDateFormat newOsmApiDateTimeFormat() {
    256         // Example: "2010-09-07 14:39:41 UTC".
    257         // Always parsed with US locale regardless of the current locale in JOSM
    258         return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US);
    259     }
    260 
    261     /**
    262253     * Returns the date format to be used for current user, based on user preferences.
    263254     * @param dateStyle The date style as described in {@link DateFormat#getDateInstance}. Ignored if "ISO dates" option is set
     
    367358
    368359    /**
     360     * Differs from {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} by using ' ' instead of 'T' to separate date/time.
     361     */
     362    private static final DateTimeFormatter ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder()
     363                .parseCaseInsensitive()
     364                .append(DateTimeFormatter.ISO_LOCAL_DATE)
     365                .appendLiteral(' ')
     366                .append(DateTimeFormatter.ISO_LOCAL_TIME)
     367                .toFormatter();
     368
     369    /**
    369370     * Returns the date/time formatter to be used for current user, based on user preferences.
    370371     * @param dateStyle The date style. Ignored if "ISO dates" option is set.
     
    374375    public static DateTimeFormatter getDateTimeFormatter(FormatStyle dateStyle, FormatStyle timeStyle) {
    375376        DateTimeFormatter formatter = PROP_ISO_DATES.get()
    376                 ? DateTimeFormatter.ISO_LOCAL_DATE_TIME
     377                ? ISO_LOCAL_DATE_TIME
    377378                : DateTimeFormatter.ofLocalizedDateTime(dateStyle, timeStyle);
    378379        return formatter.withZone(ZoneId.systemDefault());
  • trunk/test/unit/org/openstreetmap/josm/tools/ExceptionUtilTest.java

    r17275 r17840  
    110110
    111111        assertEquals("<html>Failed to upload to changeset <strong>1</strong><br>because it has already been closed on 2016-01-01 00:00:00.",
    112                 ExceptionUtil.explainChangesetClosedException(new ChangesetClosedException(1, DateUtils.fromString("2016-01-01"), null)));
     112                ExceptionUtil.explainChangesetClosedException(new ChangesetClosedException(1, DateUtils.parseInstant("2016-01-01"), null)));
    113113    }
    114114
Note: See TracChangeset for help on using the changeset viewer.