Class DateUtils
- java.lang.Object
-
- org.openstreetmap.josm.tools.date.DateUtils
-
public final class DateUtils extends java.lang.Object
A static utility class dealing with:- parsing XML date quickly and formatting a date to the XML UTC format regardless of current locale
- providing a single entry point for formatting dates to be displayed in JOSM GUI, based on user preferences
-
-
Field Summary
Fields Modifier and Type Field Description private static java.time.format.DateTimeFormatterISO_LOCAL_DATE_TIMEDiffers fromDateTimeFormatter.ISO_LOCAL_DATE_TIMEby using ' ' instead of 'T' to separate date/time.static BooleanPropertyPROP_ISO_DATESProperty to enable display of ISO dates globally.static java.util.TimeZoneUTCThe UTC time zone.
-
Constructor Summary
Constructors Modifier Constructor Description privateDateUtils()Constructs a newDateUtils.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static booleancheckLayout(java.lang.String text, java.lang.String pattern)Check text for a specified layoutstatic java.util.DatecloneDate(java.util.Date d)Null-safe date cloning method.static java.lang.StringformatDate(java.util.Date date, int dateStyle)Formats a date to be displayed to current user, based on user preferences.static java.lang.StringformatDateTime(java.util.Date datetime, int dateStyle, int timeStyle)Formats a date/time to be displayed to current user, based on user preferences.static java.lang.StringformatTime(java.util.Date time, int timeStyle)Formats a time to be displayed to current user, based on user preferences.static java.lang.StringfromDate(java.util.Date date)Formats a date to the XML UTC format regardless of current locale.static java.util.DatefromString(java.lang.String str)Parses XML date quickly, regardless of current locale.static java.lang.StringfromTimestamp(int timestamp)Formats a date to the XML UTC format regardless of current locale.static java.lang.StringfromTimestamp(long timestamp)Formats a date to the XML UTC format regardless of current locale.static java.lang.StringfromTimestampInMillis(long timestamp)Formats a date to the XML UTC format regardless of current locale.static java.text.DateFormatgetDateFormat(int dateStyle)Returns the date format to be used for current user, based on user preferences.static java.time.format.DateTimeFormattergetDateFormatter(java.time.format.FormatStyle dateStyle)Returns the date formatter to be used for current user, based on user preferences.static java.text.DateFormatgetDateTimeFormat(int dateStyle, int timeStyle)Returns the date/time format to be used for current user, based on user preferences.static java.time.format.DateTimeFormattergetDateTimeFormatter(java.time.format.FormatStyle dateStyle, java.time.format.FormatStyle timeStyle)Returns the date/time formatter to be used for current user, based on user preferences.static java.text.DateFormatgetTimeFormat(int timeStyle)Returns the time format to be used for current user, based on user preferences.static java.time.format.DateTimeFormattergetTimeFormatter(java.time.format.FormatStyle timeStyle)Returns the time formatter to be used for current user, based on user preferences.static java.text.SimpleDateFormatnewIsoDateFormat()Returns a newSimpleDateFormatfor date only, according to ISO 8601.private static intnum(char c)static java.time.InstantparseInstant(java.lang.String str)Parses the given date string quickly, regardless of current locale.private static intparsePart2(java.lang.String str, int off)private static intparsePart3(java.lang.String str, int off)private static intparsePart4(java.lang.String str, int off)private static intparsePart6(java.lang.String str, int off)static longtsFromString(java.lang.String str)Parses XML date quickly, regardless of current locale.
-
-
-
Field Detail
-
UTC
public static final java.util.TimeZone UTC
The UTC time zone.
-
PROP_ISO_DATES
public static final BooleanProperty PROP_ISO_DATES
Property to enable display of ISO dates globally.- Since:
- 7299
-
ISO_LOCAL_DATE_TIME
private static final java.time.format.DateTimeFormatter ISO_LOCAL_DATE_TIME
Differs fromDateTimeFormatter.ISO_LOCAL_DATE_TIMEby using ' ' instead of 'T' to separate date/time.
-
-
Constructor Detail
-
DateUtils
private DateUtils()
Constructs a newDateUtils.
-
-
Method Detail
-
fromString
public static java.util.Date fromString(java.lang.String str)
Parses XML date quickly, regardless of current locale.- Parameters:
str- The XML date as string- Returns:
- The date
- Throws:
UncheckedParseException- if the date does not match any of the supported date formatsjava.time.DateTimeException- if the value of any field is out of range, or if the day-of-month is invalid for the month-year
-
tsFromString
public static long tsFromString(java.lang.String str)
Parses XML date quickly, regardless of current locale.- Parameters:
str- The XML date as string- Returns:
- The date in milliseconds since epoch
- Throws:
UncheckedParseException- if the date does not match any of the supported date formatsjava.time.DateTimeException- if the value of any field is out of range, or if the day-of-month is invalid for the month-year
-
parseInstant
public static java.time.Instant parseInstant(java.lang.String str)
Parses the given date string quickly, regardless of current locale.- Parameters:
str- the date string- Returns:
- the parsed instant
- Throws:
UncheckedParseException- if the date does not match any of the supported date formats
-
fromTimestamp
public static java.lang.String fromTimestamp(long timestamp)
Formats a date to the XML UTC format regardless of current locale.- Parameters:
timestamp- number of seconds since the epoch- Returns:
- The formatted date
- Since:
- 14055
-
fromTimestampInMillis
public static java.lang.String fromTimestampInMillis(long timestamp)
Formats a date to the XML UTC format regardless of current locale.- Parameters:
timestamp- number of milliseconds since the epoch- Returns:
- The formatted date
- Since:
- 14434
-
fromTimestamp
public static java.lang.String fromTimestamp(int timestamp)
Formats a date to the XML UTC format regardless of current locale.- Parameters:
timestamp- number of seconds since the epoch- Returns:
- The formatted date
-
fromDate
public static java.lang.String fromDate(java.util.Date date)
Formats a date to the XML UTC format regardless of current locale.- Parameters:
date- The date to format- Returns:
- The formatted date
-
cloneDate
public static java.util.Date cloneDate(java.util.Date d)
Null-safe date cloning method.- Parameters:
d- date to clone, or null- Returns:
- cloned date, or null
- Since:
- 11878
-
checkLayout
private static boolean checkLayout(java.lang.String text, java.lang.String pattern)
Check text for a specified layout- Parameters:
text- The text to checkpattern- The pattern to use- Returns:
trueif the layout matches, otherwisefalse
-
num
private static int num(char c)
-
parsePart2
private static int parsePart2(java.lang.String str, int off)
-
parsePart3
private static int parsePart3(java.lang.String str, int off)
-
parsePart4
private static int parsePart4(java.lang.String str, int off)
-
parsePart6
private static int parsePart6(java.lang.String str, int off)
-
newIsoDateFormat
public static java.text.SimpleDateFormat newIsoDateFormat()
Returns a newSimpleDateFormatfor date only, according to ISO 8601.- Returns:
- a new ISO 8601 date format, for date only.
- Since:
- 7299
-
getDateFormat
public static java.text.DateFormat getDateFormat(int dateStyle)
Returns the date format to be used for current user, based on user preferences.- Parameters:
dateStyle- The date style as described inDateFormat.getDateInstance(). Ignored if "ISO dates" option is set- Returns:
- The date format
- Since:
- 7299
-
getDateFormatter
public static java.time.format.DateTimeFormatter getDateFormatter(java.time.format.FormatStyle dateStyle)
Returns the date formatter to be used for current user, based on user preferences.- Parameters:
dateStyle- The date style. Ignored if "ISO dates" option is set.- Returns:
- The date format
-
formatDate
public static java.lang.String formatDate(java.util.Date date, int dateStyle)
Formats a date to be displayed to current user, based on user preferences.- Parameters:
date- The date to display. Must not benulldateStyle- The date style as described inDateFormat.getDateInstance(). Ignored if "ISO dates" option is set- Returns:
- The formatted date
- Since:
- 7299
-
getTimeFormat
public static java.text.DateFormat getTimeFormat(int timeStyle)
Returns the time format to be used for current user, based on user preferences.- Parameters:
timeStyle- The time style as described inDateFormat.getTimeInstance(). Ignored if "ISO dates" option is set- Returns:
- The time format
- Since:
- 7299
-
getTimeFormatter
public static java.time.format.DateTimeFormatter getTimeFormatter(java.time.format.FormatStyle timeStyle)
Returns the time formatter to be used for current user, based on user preferences.- Parameters:
timeStyle- The time style. Ignored if "ISO dates" option is set.- Returns:
- The time format
-
formatTime
public static java.lang.String formatTime(java.util.Date time, int timeStyle)
Formats a time to be displayed to current user, based on user preferences.- Parameters:
time- The time to display. Must not benulltimeStyle- The time style as described inDateFormat.getTimeInstance(). Ignored if "ISO dates" option is set- Returns:
- The formatted time
- Since:
- 7299
-
getDateTimeFormat
public static java.text.DateFormat getDateTimeFormat(int dateStyle, int timeStyle)
Returns the date/time format to be used for current user, based on user preferences.- Parameters:
dateStyle- The date style as described inDateFormat.getDateTimeInstance(). Ignored if "ISO dates" option is settimeStyle- The time style as described inDateFormat.getDateTimeInstance. Ignored if "ISO dates" option is set- Returns:
- The date/time format
- Since:
- 7299
-
getDateTimeFormatter
public static java.time.format.DateTimeFormatter getDateTimeFormatter(java.time.format.FormatStyle dateStyle, java.time.format.FormatStyle timeStyle)
Returns the date/time formatter to be used for current user, based on user preferences.- Parameters:
dateStyle- The date style. Ignored if "ISO dates" option is set.timeStyle- The time style. Ignored if "ISO dates" option is set.- Returns:
- The date/time format
-
formatDateTime
public static java.lang.String formatDateTime(java.util.Date datetime, int dateStyle, int timeStyle)
Formats a date/time to be displayed to current user, based on user preferences.- Parameters:
datetime- The date/time to display. Must not benulldateStyle- The date style as described inDateFormat.getDateTimeInstance(). Ignored if "ISO dates" option is settimeStyle- The time style as described inDateFormat.getDateTimeInstance. Ignored if "ISO dates" option is set- Returns:
- The formatted date/time
- Since:
- 7299
-
-