Ignore:
Timestamp:
2014-07-09T22:15:41+02:00 (6 years ago)
Author:
Don-vip
Message:

fix #10121 - Add a new look-and-feel preference to display ISO 8601 dates globally

Location:
trunk/src/org/openstreetmap/josm/tools/date
Files:
2 added
3 moved

Legend:

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

    r7293 r7299  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.tools;
    3 
     2package org.openstreetmap.josm.tools.date;
     3
     4import java.text.DateFormat;
    45import java.text.ParsePosition;
    56import java.text.SimpleDateFormat;
     
    78import java.util.Date;
    89import java.util.GregorianCalendar;
     10import java.util.Locale;
    911import java.util.TimeZone;
    1012
     
    1416
    1517import org.openstreetmap.josm.Main;
     18import org.openstreetmap.josm.data.preferences.BooleanProperty;
     19import org.openstreetmap.josm.tools.CheckParameterUtil;
    1620
    1721/**
    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>
    2127 * @author nenik
    2228 */
    2329public 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
    2541    /**
    2642     * A shared instance used for conversion between individual date fields
     
    4460    }
    4561
     62    /**
     63     * Parses XML date quickly, regardless of current locale.
     64     * @param str The XML date as string
     65     * @return The date
     66     */
    4667    public static synchronized Date fromString(String str) {
    4768        // "2007-07-25T09:26:24{Z|{+|-}01:00}"
     
    6586
    6687            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") ||
    6989                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") ||
    7090                checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") ||
     
    83103
    84104            return calendar.getTime();
    85         }
    86         else
    87         {
     105        } else {
    88106            // example date format "18-AUG-08 13:33:03"
    89107            SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
     
    100118    }
    101119
     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     */
    102125    public static synchronized String fromDate(Date date) {
    103126        calendar.setTime(date);
     
    122145    }
    123146
     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    }
    124257}
  • trunk/src/org/openstreetmap/josm/tools/date/FallbackDateParser.java

    r7293 r7299  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.tools;
     2package org.openstreetmap.josm.tools.date;
    33
    44import java.text.DateFormat;
     
    1616 * @author Brett Henderson
    1717 */
    18 public class FallbackDateParser {
     18class FallbackDateParser {
    1919
    2020    private static final String[] formats = {
  • trunk/src/org/openstreetmap/josm/tools/date/PrimaryDateParser.java

    r7293 r7299  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.tools;
     2package org.openstreetmap.josm.tools.date;
    33
    44import java.text.ParseException;
Note: See TracChangeset for help on using the changeset viewer.