Class DateUtils
- java.lang.Object
-
- org.openstreetmap.josm.tools.date.DateUtils
-
-
Field Summary
Fields Modifier and Type Field Description private static DateTimeFormatter
ISO_LOCAL_DATE_TIME
Differs fromDateTimeFormatter.ISO_LOCAL_DATE_TIME
by using ' ' instead of 'T' to separate date/time.static BooleanProperty
PROP_ISO_DATES
Property to enable display of ISO dates globally.static TimeZone
UTC
The UTC time zone.
-
Constructor Summary
Constructors Modifier Constructor Description private
DateUtils()
Constructs a newDateUtils
.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static boolean
checkLayout(String text, String pattern)
static Date
cloneDate(Date d)
Null-safe date cloning method.static String
formatDate(Date date, int dateStyle)
Formats a date to be displayed to current user, based on user preferences.static String
formatDateTime(Date datetime, int dateStyle, int timeStyle)
Formats a date/time to be displayed to current user, based on user preferences.static String
formatTime(Date time, int timeStyle)
Formats a time to be displayed to current user, based on user preferences.static String
fromDate(Date date)
Formats a date to the XML UTC format regardless of current locale.static Date
fromString(String str)
Parses XML date quickly, regardless of current locale.static String
fromTimestamp(int timestamp)
Formats a date to the XML UTC format regardless of current locale.static String
fromTimestamp(long timestamp)
Formats a date to the XML UTC format regardless of current locale.static String
fromTimestampInMillis(long timestamp)
Formats a date to the XML UTC format regardless of current locale.static DateFormat
getDateFormat(int dateStyle)
Returns the date format to be used for current user, based on user preferences.static DateTimeFormatter
getDateFormatter(FormatStyle dateStyle)
Returns the date formatter to be used for current user, based on user preferences.static DateFormat
getDateTimeFormat(int dateStyle, int timeStyle)
Returns the date/time format to be used for current user, based on user preferences.static DateTimeFormatter
getDateTimeFormatter(FormatStyle dateStyle, FormatStyle timeStyle)
Returns the date/time formatter to be used for current user, based on user preferences.static DateFormat
getTimeFormat(int timeStyle)
Returns the time format to be used for current user, based on user preferences.static DateTimeFormatter
getTimeFormatter(FormatStyle timeStyle)
Returns the time formatter to be used for current user, based on user preferences.static SimpleDateFormat
newIsoDateFormat()
Returns a newSimpleDateFormat
for date only, according to ISO 8601.private static int
num(char c)
static Instant
parseInstant(String str)
Parses the given date string quickly, regardless of current locale.private static int
parsePart2(String str, int off)
private static int
parsePart3(String str, int off)
private static int
parsePart4(String str, int off)
private static int
parsePart6(String str, int off)
static long
tsFromString(String str)
Parses XML date quickly, regardless of current locale.
-
-
-
Field Detail
-
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 DateTimeFormatter ISO_LOCAL_DATE_TIME
Differs fromDateTimeFormatter.ISO_LOCAL_DATE_TIME
by using ' ' instead of 'T' to separate date/time.
-
-
Constructor Detail
-
DateUtils
private DateUtils()
Constructs a newDateUtils
.
-
-
Method Detail
-
fromString
public static Date fromString(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 formatsDateTimeException
- 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(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 formatsDateTimeException
- 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 Instant parseInstant(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 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 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 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 String fromDate(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 Date cloneDate(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(String text, String pattern)
-
num
private static int num(char c)
-
parsePart2
private static int parsePart2(String str, int off)
-
parsePart3
private static int parsePart3(String str, int off)
-
parsePart4
private static int parsePart4(String str, int off)
-
parsePart6
private static int parsePart6(String str, int off)
-
newIsoDateFormat
public static SimpleDateFormat newIsoDateFormat()
Returns a newSimpleDateFormat
for date only, according to ISO 8601.- Returns:
- a new ISO 8601 date format, for date only.
- Since:
- 7299
-
getDateFormat
public static 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 DateTimeFormatter getDateFormatter(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 String formatDate(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 benull
dateStyle
- The date style as described inDateFormat.getDateInstance()
. Ignored if "ISO dates" option is set- Returns:
- The formatted date
- Since:
- 7299
-
getTimeFormat
public static 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 DateTimeFormatter getTimeFormatter(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 String formatTime(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 benull
timeStyle
- The time style as described inDateFormat.getTimeInstance()
. Ignored if "ISO dates" option is set- Returns:
- The formatted time
- Since:
- 7299
-
getDateTimeFormat
public static 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 DateTimeFormatter getDateTimeFormatter(FormatStyle dateStyle, 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 String formatDateTime(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 benull
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 formatted date/time
- Since:
- 7299
-
-