Changeset 11035 in josm
- Timestamp:
- 2016-09-20T22:46:47+02:00 (8 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/query/AdvancedChangesetQueryPanel.java
r10626 r11035 11 11 import java.awt.event.ItemEvent; 12 12 import java.awt.event.ItemListener; 13 import java.text.DateFormat; 14 import java.text.ParseException; 13 import java.time.LocalDate; 14 import java.time.LocalTime; 15 import java.time.ZoneId; 16 import java.time.ZonedDateTime; 17 import java.time.format.DateTimeFormatter; 18 import java.time.format.DateTimeParseException; 19 import java.time.format.FormatStyle; 15 20 import java.util.Date; 16 import java.util.GregorianCalendar;17 import java.util.Locale;18 21 19 22 import javax.swing.BorderFactory; … … 842 845 throw new IllegalStateException(tr("Cannot build changeset query with time based restrictions. Input is not valid.")); 843 846 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); 849 851 } 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()); 855 855 856 856 d1 = valCreatedBeforeDate.getDate(); 857 857 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()); 860 859 861 860 query.closedAfterAndCreatedBefore(d3, d4); … … 1042 1041 1043 1042 public String getStandardTooltipText() { 1044 Date date = new Date();1043 final ZonedDateTime now = ZonedDateTime.now(); 1045 1044 return tr( 1046 1045 "Please enter a date in the usual format for your locale.<br>" … … 1049 1048 + "Example: {2}<br>" 1050 1049 + "Example: {3}<br>", 1051 Date Format.getDateInstance(DateFormat.SHORT, Locale.getDefault()).format(date),1052 Date Format.getDateInstance(DateFormat.MEDIUM, Locale.getDefault()).format(date),1053 Date Format.getDateInstance(DateFormat.LONG, Locale.getDefault()).format(date),1054 Date Format.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) 1055 1054 ); 1056 1055 } … … 1068 1067 } 1069 1068 1070 public Date getDate() {1071 for ( int format: new int[] {DateFormat.SHORT, DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL}) {1072 Date Format df = DateFormat.getDateInstance(format);1069 public LocalDate getDate() { 1070 for (final FormatStyle format: FormatStyle.values()) { 1071 DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(format); 1073 1072 try { 1074 return df.parse(getComponent().getText());1075 } catch ( ParseException e) {1073 return LocalDate.parse(getComponent().getText(), df); 1074 } catch (DateTimeParseException e) { 1076 1075 // Try next format 1077 1076 Main.trace(e); … … 1109 1108 1110 1109 public String getStandardTooltipText() { 1111 Date date = new Date();1110 final ZonedDateTime now = ZonedDateTime.now(); 1112 1111 return tr( 1113 1112 "Please enter a valid time in the usual format for your locale.<br>" … … 1116 1115 + "Example: {2}<br>" 1117 1116 + "Example: {3}<br>", 1118 Date Format.getTimeInstance(DateFormat.SHORT, Locale.getDefault()).format(date),1119 Date Format.getTimeInstance(DateFormat.MEDIUM, Locale.getDefault()).format(date),1120 Date Format.getTimeInstance(DateFormat.LONG, Locale.getDefault()).format(date),1121 Date Format.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) 1122 1121 ); 1123 1122 } … … 1135 1134 } 1136 1135 1137 public Date getDate() {1136 public LocalTime getDate() { 1138 1137 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); 1142 1142 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); 1146 1147 } 1147 1148 } 1148 return null;1149 return LocalTime.MIDNIGHT; 1149 1150 } 1150 1151 } -
trunk/src/org/openstreetmap/josm/gui/layer/gpx/DateFilterPanel.java
r10611 r11035 7 7 import java.awt.GridBagLayout; 8 8 import java.awt.event.ActionListener; 9 import java.time.ZoneId; 10 import java.time.ZonedDateTime; 9 11 import java.util.Date; 10 import java.util.GregorianCalendar;11 12 12 13 import javax.swing.JCheckBox; … … 49 50 final Date startTime, endTime; 50 51 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]; 52 53 endTime = (bounds.length == 0) ? new Date() : bounds[1]; 53 54 -
trunk/src/org/openstreetmap/josm/io/GpxExporter.java
r10615 r11035 12 12 import java.io.OutputStream; 13 13 import java.text.MessageFormat; 14 import java. util.Calendar;14 import java.time.Year; 15 15 16 16 import javax.swing.JButton; … … 225 225 String sCopyrightYear = data.getString(META_COPYRIGHT_YEAR); 226 226 if (sCopyrightYear == null) { 227 sCopyrightYear = Integer.toString(Calendar.getInstance().get(Calendar.YEAR));227 sCopyrightYear = Year.now().toString(); 228 228 } 229 229 copyrightYear.setText(sCopyrightYear); -
trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java
r10513 r11035 5 5 import java.text.ParsePosition; 6 6 import java.text.SimpleDateFormat; 7 import java.util.Calendar; 7 import java.time.Instant; 8 import java.time.ZoneId; 9 import java.time.ZoneOffset; 10 import java.time.ZonedDateTime; 11 import java.time.format.DateTimeFormatter; 8 12 import java.util.Date; 9 import java.util.GregorianCalendar;10 13 import java.util.Locale; 11 14 import java.util.TimeZone; … … 13 16 import javax.xml.datatype.DatatypeConfigurationException; 14 17 import javax.xml.datatype.DatatypeFactory; 15 import javax.xml.datatype.XMLGregorianCalendar;16 18 17 19 import org.openstreetmap.josm.Main; … … 41 43 public static final BooleanProperty PROP_ISO_DATES = new BooleanProperty("iso.dates", false); 42 44 43 /**44 * A shared instance used for conversion between individual date fields45 * and long millis time. It is guarded against conflict by the class lock.46 * The shared instance is used because the construction, together47 * with the timezone lookup, is very expensive.48 */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 */53 private static final GregorianCalendar calendarLocale = new GregorianCalendar(TimeZone.getDefault());54 45 private static final DatatypeFactory XML_DATE; 55 46 56 47 static { 57 calendar.setTimeInMillis(0);58 calendarLocale.setTimeInMillis(0);59 60 48 DatatypeFactory fact = null; 61 49 try { … … 97 85 checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") || 98 86 checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) { 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 } 105 c.set( 87 final ZonedDateTime local = ZonedDateTime.of( 106 88 parsePart4(str, 0), 107 parsePart2(str, 5) -1,89 parsePart2(str, 5), 108 90 parsePart2(str, 8), 109 91 parsePart2(str, 11), 110 92 parsePart2(str, 14), 111 parsePart2(str, 17)); 112 c.set(Calendar.MILLISECOND, 0); 113 93 parsePart2(str, 17), 94 0, 95 // consider EXIF date in default timezone 96 checkLayout(str, "xxxx:xx:xx xx:xx:xx") ? ZoneId.systemDefault() : ZoneOffset.UTC 97 ); 114 98 if (str.length() == 22 || str.length() == 25) { 115 int plusHr = parsePart2(str, 20);116 int mul = str.charAt(19) == '+' ? -3600000 : 3600000;117 return c.getTimeInMillis()+plusHr*mul;99 final int plusHr = parsePart2(str, 20); 100 final int mul = str.charAt(19) == '+' ? -1 : 1; 101 return local.plusHours(plusHr * mul).toInstant().toEpochMilli(); 118 102 } 119 120 return c.getTimeInMillis(); 103 return local.toInstant().toEpochMilli(); 121 104 } else if (checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") || 122 105 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") || … … 124 107 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") || 125 108 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) { 126 // consider EXIF date in default timezone 127 final Calendar c = checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? getLocalCalendar() : calendar; 128 c.set( 109 final ZonedDateTime local = ZonedDateTime.of( 129 110 parsePart4(str, 0), 130 parsePart2(str, 5) -1,111 parsePart2(str, 5), 131 112 parsePart2(str, 8), 132 113 parsePart2(str, 11), 133 114 parsePart2(str, 14), 134 parsePart2(str, 17)); 135 c.set(Calendar.MILLISECOND, 0); 136 long millis = parsePart3(str, 20); 115 parsePart2(str, 17), 116 parsePart3(str, 20) * 1_000_000, 117 // consider EXIF date in default timezone 118 checkLayout(str, "xxxx:xx:xx xx:xx:xx.xxx") ? ZoneId.systemDefault() : ZoneOffset.UTC 119 ); 137 120 if (str.length() == 29) { 138 millis += parsePart2(str, 24) * (str.charAt(23) == '+' ? -3600000 : 3600000); 121 final int plusHr = parsePart2(str, 24); 122 final int mul = str.charAt(23) == '+' ? -1 : 1; 123 return local.plusHours(plusHr * mul).toInstant().toEpochMilli(); 139 124 } 140 141 return c.getTimeInMillis() + millis; 125 return local.toInstant().toEpochMilli(); 142 126 } else { 143 127 // example date format "18-AUG-08 13:33:03" … … 155 139 } 156 140 157 private static Calendar getLocalCalendar() {158 final Calendar c = calendarLocale;159 c.setTimeZone(TimeZone.getDefault());160 return c;161 }162 163 private static String toXmlFormat(GregorianCalendar cal) {164 XMLGregorianCalendar xgc = XML_DATE.newXMLGregorianCalendar(cal);165 if (cal.get(Calendar.MILLISECOND) == 0) {166 xgc.setFractionalSecond(null);167 }168 return xgc.toXMLFormat();169 }170 171 141 /** 172 142 * Formats a date to the XML UTC format regardless of current locale. … … 175 145 */ 176 146 public static synchronized String fromTimestamp(int timestamp) { 177 calendar.setTimeInMillis(timestamp * 1000L);178 return toXmlFormat(calendar);147 final ZonedDateTime temporal = Instant.ofEpochMilli(timestamp * 1000L).atZone(ZoneOffset.UTC); 148 return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(temporal); 179 149 } 180 150 … … 185 155 */ 186 156 public static synchronized String fromDate(Date date) { 187 calendar.setTime(date);188 return toXmlFormat(calendar);157 final ZonedDateTime temporal = date.toInstant().atZone(ZoneOffset.UTC); 158 return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(temporal); 189 159 } 190 160 -
trunk/test/unit/org/openstreetmap/josm/data/AutosaveTaskTest.java
r10467 r11035 14 14 import java.nio.file.Files; 15 15 import java.nio.file.Paths; 16 import java.util.Calendar; 16 import java.time.ZoneId; 17 import java.time.ZonedDateTime; 17 18 import java.util.Date; 18 19 import java.util.List; … … 87 88 Files.createDirectories(task.getAutosaveDir()); 88 89 AutosaveLayerInfo info = new AutosaveLayerInfo(new OsmDataLayer(new DataSet(), "layer", null)); 89 Calendar cal = Calendar.getInstance(); 90 cal.set(2016, 0, 1, 1, 2, 3); 91 cal.set(Calendar.MILLISECOND, 456); 92 Date fixed = cal.getTime(); 90 Date fixed = Date.from(ZonedDateTime.of(2016, 1, 1, 1, 2, 3, 456_000_000, ZoneId.systemDefault()).toInstant()); 93 91 94 92 AutosaveTask.PROP_INDEX_LIMIT.put(5); -
trunk/test/unit/org/openstreetmap/josm/data/osm/DataSetMergerTest.java
r10945 r11035 11 11 12 12 import java.io.StringWriter; 13 import java.time.Instant; 13 14 import java.util.Arrays; 14 import java.util.Calendar;15 15 import java.util.Date; 16 import java.util.GregorianCalendar;17 16 18 17 import org.junit.After; … … 26 25 27 26 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 28 29 27 /** 30 28 * Unit tests for class {@link DataSetMerger}. … … 298 296 public void testNodeSimpleNoIdSemanticallyEqual() { 299 297 300 Calendar cal = GregorianCalendar.getInstance();301 298 User myUser = User.createOsmUser(1111, "my"); 302 299 … … 307 304 n.put("key1", "value1"); 308 305 n.setUser(myUser); 309 n.setTimestamp( cal.getTime());306 n.setTimestamp(new Date()); 310 307 311 308 my.addPrimitive(n); … … 314 311 n1.setCoor(LatLon.ZERO); 315 312 n1.put("key1", "value1"); 316 cal.add(Calendar.HOUR, 1); 317 Date timestamp = cal.getTime(); 318 n1.setTimestamp(timestamp); 313 n1.setTimestamp(Date.from(Instant.now().plusSeconds(3600))); 319 314 n1.setUser(theirUser); 320 315 their.addPrimitive(n1); -
trunk/test/unit/org/openstreetmap/josm/io/ChangesetQueryUrlParserTest.groovy
r10222 r11035 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.io; 3 4 import static org.junit.Assert.* 2 package org.openstreetmap.josm.io 5 3 6 4 import org.junit.Test … … 8 6 import org.openstreetmap.josm.io.ChangesetQuery.ChangesetQueryUrlParser 9 7 8 import java.time.OffsetDateTime 9 import java.time.ZoneOffset 10 10 11 class ChangesetQueryUrlParserTest { 11 12 final shouldFail = new GroovyTestCase().&shouldFail … … 127 128 assert q != null 128 129 assert q.@closedAfter != null 129 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+0")); 130 cal.setTime(q.@closedAfter); 131 assert cal.get(Calendar.YEAR) == 2009 132 assert cal.get(Calendar.MONTH) == 11 // calendar is 0-based 133 assert cal.get(Calendar.DAY_OF_MONTH) == 25 134 assert cal.get(Calendar.HOUR_OF_DAY) == 10 135 assert cal.get(Calendar.MINUTE) == 0 136 assert cal.get(Calendar.SECOND) == 0 130 def cal = q.@closedAfter.toInstant().atOffset(ZoneOffset.UTC) 131 assert cal == OffsetDateTime.of(2009, 12, 25, 10, 0, 0, 0, ZoneOffset.UTC) 137 132 138 133 // OK -
trunk/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java
r10569 r11035 10 10 import java.text.ParseException; 11 11 import java.text.SimpleDateFormat; 12 import java.util.Calendar; 12 import java.time.ZoneId; 13 import java.time.ZonedDateTime; 13 14 import java.util.Date; 14 import java.util.GregorianCalendar;15 15 import java.util.TimeZone; 16 16 … … 24 24 25 25 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; 26 27 26 /** 28 27 * EXIF metadata extraction test … … 55 54 public void testReadTime() throws ParseException { 56 55 Date date = ExifReader.readTime(directionSampleFile); 57 assertEquals( new GregorianCalendar(2010, Calendar.MAY, 15, 17, 12, 05).getTime(), date);56 assertEquals(ZonedDateTime.of(2010, 5, 15, 17, 12, 5, 0, ZoneId.systemDefault()).toInstant(), date.toInstant()); 58 57 59 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin")); 58 final TimeZone zone = TimeZone.getTimeZone("Europe/Berlin"); 59 TimeZone.setDefault(zone); 60 60 date = ExifReader.readTime(directionSampleFile); 61 61 TimeZone.setDefault(DateUtils.UTC); 62 assertEquals( new GregorianCalendar(2010, Calendar.MAY, 15, 15, 12, 05).getTime(), date);62 assertEquals(ZonedDateTime.of(2010, 5, 15, 17, 12, 5, 0, zone.toZoneId()).toInstant(), date.toInstant()); 63 63 } 64 64
Note:
See TracChangeset
for help on using the changeset viewer.