Ignore:
Timestamp:
2016-09-20T22:46:47+02:00 (8 years ago)
Author:
simon04
Message:

see #13376 - Replace Calendar usages with Java 8 Date API

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/query/AdvancedChangesetQueryPanel.java

    r10626 r11035  
    1111import java.awt.event.ItemEvent;
    1212import java.awt.event.ItemListener;
    13 import java.text.DateFormat;
    14 import java.text.ParseException;
     13import java.time.LocalDate;
     14import java.time.LocalTime;
     15import java.time.ZoneId;
     16import java.time.ZonedDateTime;
     17import java.time.format.DateTimeFormatter;
     18import java.time.format.DateTimeParseException;
     19import java.time.format.FormatStyle;
    1520import java.util.Date;
    16 import java.util.GregorianCalendar;
    17 import java.util.Locale;
    1821
    1922import javax.swing.BorderFactory;
     
    842845                throw new IllegalStateException(tr("Cannot build changeset query with time based restrictions. Input is not valid."));
    843846            if (rbClosedAfter.isSelected()) {
    844                 GregorianCalendar cal = new GregorianCalendar();
    845                 Date d1 = valClosedAfterDate1.getDate();
    846                 Date d2 = valClosedAfterTime1.getDate();
    847                 cal.setTimeInMillis(d1.getTime() + (d2 == null ? 0 : d2.getTime()));
    848                 query.closedAfter(cal.getTime());
     847                LocalDate d1 = valClosedAfterDate1.getDate();
     848                LocalTime d2 = valClosedAfterTime1.getDate();
     849                final Date d3 = new Date(d1.atTime(d2).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
     850                query.closedAfter(d3);
    849851            } else if (rbClosedAfterAndCreatedBefore.isSelected()) {
    850                 GregorianCalendar cal = new GregorianCalendar();
    851                 Date d1 = valClosedAfterDate2.getDate();
    852                 Date d2 = valClosedAfterTime2.getDate();
    853                 cal.setTimeInMillis(d1.getTime() + (d2 == null ? 0 : d2.getTime()));
    854                 Date d3 = cal.getTime();
     852                LocalDate d1 = valClosedAfterDate2.getDate();
     853                LocalTime d2 = valClosedAfterTime2.getDate();
     854                Date d3 = new Date(d1.atTime(d2).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
    855855
    856856                d1 = valCreatedBeforeDate.getDate();
    857857                d2 = valCreatedBeforeTime.getDate();
    858                 cal.setTimeInMillis(d1.getTime() + (d2 == null ? 0 : d2.getTime()));
    859                 Date d4 = cal.getTime();
     858                Date d4 = new Date(d1.atTime(d2).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
    860859
    861860                query.closedAfterAndCreatedBefore(d3, d4);
     
    10421041
    10431042        public String getStandardTooltipText() {
    1044             Date date = new Date();
     1043            final ZonedDateTime now = ZonedDateTime.now();
    10451044            return tr(
    10461045                    "Please enter a date in the usual format for your locale.<br>"
     
    10491048                    + "Example: {2}<br>"
    10501049                    + "Example: {3}<br>",
    1051                     DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(date),
    1052                     DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault()).format(date),
    1053                     DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(date),
    1054                     DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault()).format(date)
     1050                    DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(now),
     1051                    DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).format(now),
     1052                    DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).format(now),
     1053                    DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(now)
    10551054            );
    10561055        }
     
    10681067        }
    10691068
    1070         public Date getDate() {
    1071             for (int format: new int[] {DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL}) {
    1072                 DateFormat df = DateFormat.getDateInstance(format);
     1069        public LocalDate getDate() {
     1070            for (final FormatStyle format: FormatStyle.values()) {
     1071                DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(format);
    10731072                try {
    1074                     return df.parse(getComponent().getText());
    1075                 } catch (ParseException e) {
     1073                    return LocalDate.parse(getComponent().getText(), df);
     1074                } catch (DateTimeParseException e) {
    10761075                    // Try next format
    10771076                    Main.trace(e);
     
    11091108
    11101109        public String getStandardTooltipText() {
    1111             Date date = new Date();
     1110            final ZonedDateTime now = ZonedDateTime.now();
    11121111            return tr(
    11131112                    "Please enter a valid time in the usual format for your locale.<br>"
     
    11161115                    + "Example: {2}<br>"
    11171116                    + "Example: {3}<br>",
    1118                     DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(date),
    1119                     DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.getDefault()).format(date),
    1120                     DateFormat.getTimeInstance(DateFormat.LONG, Locale.getDefault()).format(date),
    1121                     DateFormat.getTimeInstance(DateFormat.FULL, Locale.getDefault()).format(date)
     1117                    DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(now),
     1118                    DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM).format(now),
     1119                    DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG).format(now),
     1120                    DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL).format(now)
    11221121            );
    11231122        }
     
    11351134        }
    11361135
    1137         public Date getDate() {
     1136        public LocalTime getDate() {
    11381137            if (getComponent().getText().trim().isEmpty())
    1139                 return null;
    1140 
    1141             for (int style : new int[]{DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL}) {
     1138                return LocalTime.MIDNIGHT;
     1139
     1140            for (final FormatStyle format: FormatStyle.values()) {
     1141                DateTimeFormatter df = DateTimeFormatter.ofLocalizedTime(format);
    11421142                try {
    1143                     return DateFormat.getTimeInstance(style, Locale.getDefault()).parse(getComponent().getText());
    1144                 } catch (ParseException e) {
    1145                     continue;
     1143                    return LocalTime.parse(getComponent().getText(), df);
     1144                } catch (DateTimeParseException e) {
     1145                    // Try next format
     1146                    Main.trace(e);
    11461147                }
    11471148            }
    1148             return null;
     1149            return LocalTime.MIDNIGHT;
    11491150        }
    11501151    }
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/DateFilterPanel.java

    r10611 r11035  
    77import java.awt.GridBagLayout;
    88import java.awt.event.ActionListener;
     9import java.time.ZoneId;
     10import java.time.ZonedDateTime;
    911import java.util.Date;
    10 import java.util.GregorianCalendar;
    1112
    1213import javax.swing.JCheckBox;
     
    4950        final Date startTime, endTime;
    5051        Date[] bounds = layer.data.getMinMaxTimeForAllTracks();
    51         startTime = (bounds.length == 0) ? new GregorianCalendar(2000, 1, 1).getTime() : bounds[0];
     52        startTime = (bounds.length == 0) ? Date.from(ZonedDateTime.of(2000, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault()).toInstant()) : bounds[0];
    5253        endTime = (bounds.length == 0) ? new Date() : bounds[1];
    5354
Note: See TracChangeset for help on using the changeset viewer.