Changeset 7299 in josm for trunk/src/org/openstreetmap/josm/tools
- Timestamp:
- 2014-07-09T22:15:41+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 2 added
- 1 deleted
- 2 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java
r7205 r7299 13 13 import java.text.DateFormat; 14 14 import java.text.ParseException; 15 import java.text.SimpleDateFormat;16 15 import java.util.Collection; 17 16 import java.util.Date; 18 import java.util.Locale;19 17 import java.util.TreeSet; 20 18 import java.util.regex.Matcher; … … 35 33 import org.openstreetmap.josm.io.OsmTransferException; 36 34 import org.openstreetmap.josm.io.auth.CredentialsManager; 35 import org.openstreetmap.josm.tools.date.DateUtils; 37 36 38 37 @SuppressWarnings("CallToThreadDumpStack") … … 362 361 if (m.matches()) { 363 362 long changesetId = Long.parseLong(m.group(1)); 364 // Example: "2010-09-07 14:39:41 UTC". Always parsed with US locale, regardless365 // of the current locale in JOSM366 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US);367 363 Date closeDate = null; 368 364 try { 369 closeDate = formatter.parse(m.group(2));365 closeDate = DateUtils.newOsmApiDateTimeFormat().parse(m.group(2)); 370 366 } catch (ParseException ex) { 371 367 Main.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2))); … … 378 374 ); 379 375 } else { 380 SimpleDateFormat dateFormat = new SimpleDateFormat();381 376 msg = tr( 382 377 "<html>Closing of changeset <strong>{0}</strong> failed<br>" 383 378 +" because it has already been closed on {1}.", 384 379 changesetId, 385 dateFormat.format(closeDate)380 DateUtils.formatDateTime(closeDate, DateFormat.DEFAULT, DateFormat.DEFAULT) 386 381 ); 387 382 } … … 408 403 */ 409 404 public static String explainChangesetClosedException(ChangesetClosedException e) { 410 SimpleDateFormat dateFormat = new SimpleDateFormat();411 405 Main.error(e); 412 406 return tr( … … 414 408 +"because it has already been closed on {1}.", 415 409 e.getChangesetId(), 416 e.getClosedOn() == null ? "?" : dateFormat.format(e.getClosedOn())410 e.getClosedOn() == null ? "?" : DateUtils.formatDateTime(e.getClosedOn(), DateFormat.DEFAULT, DateFormat.DEFAULT) 417 411 ); 418 412 } -
trunk/src/org/openstreetmap/josm/tools/ExifReader.java
r7004 r7299 9 9 import org.openstreetmap.josm.Main; 10 10 import org.openstreetmap.josm.data.coor.LatLon; 11 import org.openstreetmap.josm.tools.date.PrimaryDateParser; 11 12 12 13 import com.drew.imaging.jpeg.JpegMetadataReader; … … 57 58 if (dateStr != null) { 58 59 dateStr = dateStr.replace('/', ':'); // workaround for HTC Sensation bug, see #7228 59 return DateParser.parse(dateStr); 60 return new PrimaryDateParser().parse(dateStr); 60 61 } 61 62 } catch (ParseException e) { -
trunk/src/org/openstreetmap/josm/tools/date/DateUtils.java
r7293 r7299 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.tools; 3 2 package org.openstreetmap.josm.tools.date; 3 4 import java.text.DateFormat; 4 5 import java.text.ParsePosition; 5 6 import java.text.SimpleDateFormat; … … 7 8 import java.util.Date; 8 9 import java.util.GregorianCalendar; 10 import java.util.Locale; 9 11 import java.util.TimeZone; 10 12 … … 14 16 15 17 import org.openstreetmap.josm.Main; 18 import org.openstreetmap.josm.data.preferences.BooleanProperty; 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 16 20 17 21 /** 18 * A static utility class dealing with parsing XML date quickly and formatting 19 * a date to the XML UTC format regardless of current locale. 20 * 22 * A static utility class dealing with: 23 * <ul> 24 * <li>parsing XML date quickly and formatting a date to the XML UTC format regardless of current locale</li> 25 * <li>providing a single entry point for formatting dates to be displayed in JOSM GUI, based on user preferences</li> 26 * </ul> 21 27 * @author nenik 22 28 */ 23 29 public final class DateUtils { 24 private DateUtils() {} 30 31 private DateUtils() { 32 // Hide default constructor for utils classes 33 } 34 35 /** 36 * Property to enable display of ISO dates globally. 37 * @since 7299 38 */ 39 public static final BooleanProperty PROP_ISO_DATES = new BooleanProperty("iso.dates", false); 40 25 41 /** 26 42 * A shared instance used for conversion between individual date fields … … 44 60 } 45 61 62 /** 63 * Parses XML date quickly, regardless of current locale. 64 * @param str The XML date as string 65 * @return The date 66 */ 46 67 public static synchronized Date fromString(String str) { 47 68 // "2007-07-25T09:26:24{Z|{+|-}01:00}" … … 65 86 66 87 return calendar.getTime(); 67 } 68 else if(checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") || 88 } else if(checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") || 69 89 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") || 70 90 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") || … … 83 103 84 104 return calendar.getTime(); 85 } 86 else 87 { 105 } else { 88 106 // example date format "18-AUG-08 13:33:03" 89 107 SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss"); … … 100 118 } 101 119 120 /** 121 * Formats a date to the XML UTC format regardless of current locale. 122 * @param date The date to format 123 * @return The formatted date 124 */ 102 125 public static synchronized String fromDate(Date date) { 103 126 calendar.setTime(date); … … 122 145 } 123 146 147 /** 148 * Returns a new {@code SimpleDateFormat} for date only, according to <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a>. 149 * @return a new ISO 8601 date format, for date only. 150 * @since 7299 151 */ 152 public static final SimpleDateFormat newIsoDateFormat() { 153 return new SimpleDateFormat("yyyy-MM-dd"); 154 } 155 156 /** 157 * Returns a new {@code SimpleDateFormat} for date and time, according to <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a>. 158 * @return a new ISO 8601 date format, for date and time. 159 * @since 7299 160 */ 161 public static final SimpleDateFormat newIsoDateTimeFormat() { 162 return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); 163 } 164 165 /** 166 * Returns a new {@code SimpleDateFormat} for date and time, according to format used in OSM API errors. 167 * @return a new date format, for date and time, to use for OSM API error handling. 168 * @since 7299 169 */ 170 public static final SimpleDateFormat newOsmApiDateTimeFormat() { 171 // Example: "2010-09-07 14:39:41 UTC". 172 // Always parsed with US locale regardless of the current locale in JOSM 173 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US); 174 } 175 176 /** 177 * Returns the date format to be used for current user, based on user preferences. 178 * @param dateStyle The date style as described in {@link DateFormat#getDateInstance}. Ignored if "ISO dates" option is set 179 * @return The date format 180 * @since 7299 181 */ 182 public static final DateFormat getDateFormat(int dateStyle) { 183 if (PROP_ISO_DATES.get()) { 184 return newIsoDateFormat(); 185 } else { 186 return DateFormat.getDateInstance(dateStyle, Locale.getDefault()); 187 } 188 } 189 190 /** 191 * Formats a date to be displayed to current user, based on user preferences. 192 * @param date The date to display. Must not be {@code null} 193 * @param dateStyle The date style as described in {@link DateFormat#getDateInstance}. Ignored if "ISO dates" option is set 194 * @return The formatted date 195 * @since 7299 196 */ 197 public static final String formatDate(Date date, int dateStyle) { 198 CheckParameterUtil.ensureParameterNotNull(date, "date"); 199 return getDateFormat(dateStyle).format(date); 200 } 201 202 /** 203 * Returns the time format to be used for current user, based on user preferences. 204 * @param timeStyle The time style as described in {@link DateFormat#getTimeInstance}. Ignored if "ISO dates" option is set 205 * @return The time format 206 * @since 7299 207 */ 208 public static final DateFormat getTimeFormat(int timeStyle) { 209 if (PROP_ISO_DATES.get()) { 210 // This is not strictly conform to ISO 8601. We just want to avoid US-style times such as 3.30pm 211 return new SimpleDateFormat("HH:mm:ss"); 212 } else { 213 return DateFormat.getTimeInstance(timeStyle, Locale.getDefault()); 214 } 215 } 216 /** 217 * Formats a time to be displayed to current user, based on user preferences. 218 * @param time The time to display. Must not be {@code null} 219 * @param timeStyle The time style as described in {@link DateFormat#getTimeInstance}. Ignored if "ISO dates" option is set 220 * @return The formatted time 221 * @since 7299 222 */ 223 public static final String formatTime(Date time, int timeStyle) { 224 CheckParameterUtil.ensureParameterNotNull(time, "time"); 225 return getTimeFormat(timeStyle).format(time); 226 } 227 228 /** 229 * Returns the date/time format to be used for current user, based on user preferences. 230 * @param dateStyle The date style as described in {@link DateFormat#getDateTimeInstance}. Ignored if "ISO dates" option is set 231 * @param timeStyle The time style as described in {@code DateFormat.getDateTimeInstance}. Ignored if "ISO dates" option is set 232 * @return The date/time format 233 * @since 7299 234 */ 235 public static final DateFormat getDateTimeFormat(int dateStyle, int timeStyle) { 236 if (PROP_ISO_DATES.get()) { 237 // This is not strictly conform to ISO 8601. We just want to avoid US-style times such as 3.30pm 238 // and we don't want to use the 'T' separator as a space character is much more readable 239 return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 240 } else { 241 return DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.getDefault()); 242 } 243 } 244 245 /** 246 * Formats a date/time to be displayed to current user, based on user preferences. 247 * @param datetime The date/time to display. Must not be {@code null} 248 * @param dateStyle The date style as described in {@link DateFormat#getDateTimeInstance}. Ignored if "ISO dates" option is set 249 * @param timeStyle The time style as described in {@code DateFormat.getDateTimeInstance}. Ignored if "ISO dates" option is set 250 * @return The formatted date/time 251 * @since 7299 252 */ 253 public static final String formatDateTime(Date datetime, int dateStyle, int timeStyle) { 254 CheckParameterUtil.ensureParameterNotNull(datetime, "datetime"); 255 return getDateTimeFormat(dateStyle, timeStyle).format(datetime); 256 } 124 257 } -
trunk/src/org/openstreetmap/josm/tools/date/FallbackDateParser.java
r7293 r7299 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.tools; 2 package org.openstreetmap.josm.tools.date; 3 3 4 4 import java.text.DateFormat; … … 16 16 * @author Brett Henderson 17 17 */ 18 publicclass FallbackDateParser {18 class FallbackDateParser { 19 19 20 20 private static final String[] formats = { -
trunk/src/org/openstreetmap/josm/tools/date/PrimaryDateParser.java
r7293 r7299 1 1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.tools; 2 package org.openstreetmap.josm.tools.date; 3 3 4 4 import java.text.ParseException;
Note:
See TracChangeset
for help on using the changeset viewer.