public class DateUtil extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
DateUtil.Format
Enum representing common date formats.
|
static class |
DateUtil.TimeUnit
Represents time units in order of increasing precision.
|
| Modifier and Type | Field and Description |
|---|---|
static String[][] |
TIME_UNIT
Labels for time units.
|
| Modifier and Type | Method and Description |
|---|---|
static Date |
addDays(Date date,
int daysOffset,
boolean stripTime)
Adds specified number of days to date and, optionally strips the time component.
|
static Date |
cloneDate(Date date)
Clones a date.
|
static int |
compare(Date date1,
Date date2)
Compares two dates.
|
static Date |
endOfDay(Date date)
Returns the input date with the time set to the end of the day.
|
static String |
formatAge(Date dob)
Returns age as a formatted string expressed in days, months, or years, depending on whether
person is an infant (< 2 mos), toddler (> 2 mos, < 2 yrs), or more than 2 years old.
|
static String |
formatAge(Date dob,
boolean pluralize,
Date refDate)
Returns age as a formatted string expressed in days, months, or years, depending on whether
person is an infant (< 2 mos), toddler (> 2 mos, < 2 yrs), or more than 2 years old.
|
static String |
formatDate(Date date)
Converts a date/time value to a string, using the format dd-mmm-yyyy hh:mm.
|
static String |
formatDate(Date date,
boolean showTimezone)
Converts a date/time value to a string, using the format dd-mmm-yyyy hh:mm.
|
static String |
formatDate(Date date,
boolean showTimezone,
boolean ignoreTime)
Converts a date/time value to a string, using the format dd-mmm-yyyy hh:mm.
|
static String |
formatDate(Date date,
String timeSeparator)
Same as formatDate(Date, boolean) except replaces the time separator with the specified
string.
|
static String |
formatDuration(long duration)
Formats a duration in ms.
|
static String |
formatDuration(long duration,
DateUtil.TimeUnit accuracy)
Formats a duration in ms to the specified accuracy.
|
static String |
formatDuration(long duration,
DateUtil.TimeUnit accuracy,
boolean pluralize,
boolean abbreviated)
Formats a duration in ms to the specified accuracy.
|
static String |
formatElapsed(double elapsed)
Return elapsed time in ms to displayable format with units.
|
static String |
formatElapsed(double elapsed,
boolean pluralize,
boolean abbreviated,
boolean round)
Return elapsed time in ms to displayable format with units.
|
static String |
formatElapsed(double elapsed,
boolean pluralize,
boolean abbreviated,
boolean round,
DateUtil.TimeUnit minUnits)
Return elapsed time in ms to displayable format with units.
|
static String |
formatElapsed(double elapsed,
DateUtil.TimeUnit minUnits)
Return elapsed time in ms to displayable format with units.
|
static TimeZone |
getLocalTimeZone()
Returns the user's time zone.
|
static boolean |
hasTime(Date date)
Returns true if the date has an associated time.
|
static Date |
now()
Returns a date with the current time.
|
static Date |
parseDate(String s)
Convert a string value to a date/time.
|
static Date |
parseDate(String value,
String... patterns)
Attempts to parse an input value using one of several patterns.
|
static double |
parseElapsed(String value)
Parses an elapsed time string, returning time in milliseconds.
|
static double |
parseElapsed(String value,
DateUtil.TimeUnit units)
Parses an elapsed time string, returning time in specified units.
|
static Date |
stripTime(Date date)
Strips the time component from a date.
|
static Date |
toDate(int day,
int month,
int year)
Converts day, month, and year to a date.
|
static Date |
toDate(int day,
int month,
int year,
int hr,
int min,
int sec)
Converts day, month, year and time parameters to a date.
|
static Date |
today()
Returns a date with the current day (no time).
|
static String |
toHL7(Date date)
Convert a date to HL7 format.
|
public static String[][] TIME_UNIT
public static Date parseDate(String s)
Convert a string value to a date/time. Attempts to convert using the four locale-specific date formats (FULL, LONG, MEDIUM, SHORT). If these fail, looks to see if T+/-offset or N+/-offset is used.
TODO: probably we can make the "Java parse" portion a bit smarter by using a better variety of formats, maybe to catch Euro-style input as well.
TODO: probably we can add something like "t+d" or "t-y" as valid cases; in these scenarios, the coefficient was omitted and could be defaulted to 1.
s - String containing value to be converted.Date object corresponding to the input value, or null if
the parsing failed to resolve a valid Date.public static Date parseDate(String value, String... patterns) throws ParseException
value - String to parse.patterns - Patterns to be tried in succession until parsing succeeds.ParseException - Date parsing exception.public static Date cloneDate(Date date)
date - Date to clone.public static Date addDays(Date date, int daysOffset, boolean stripTime)
date - Date value to process.daysOffset - # of days to add.stripTime - If true, strip the time component.public static Date stripTime(Date date)
date - Original date.public static Date endOfDay(Date date)
date - Original date.public static Date now()
public static Date today()
public static int compare(Date date1, Date date2)
date1 - First date to compare.date2 - Second date to compare.public static String formatDate(Date date)
date - Date value to convert.public static String formatDate(Date date, boolean showTimezone)
date - Date value to convert.showTimezone - If true, time zone information is also appended.public static String formatDate(Date date, boolean showTimezone, boolean ignoreTime)
date - Date value to convertshowTimezone - If true, time zone information is also appended.ignoreTime - If true, the time component is ignored.public static String formatDate(Date date, String timeSeparator)
date - Date value to converttimeSeparator - String to use in place of default time separatorpublic static String toHL7(Date date)
date - Date to convert.public static boolean hasTime(Date date)
date - Date value to check.public static String formatElapsed(double elapsed)
elapsed - Elapsed time in ms.public static String formatElapsed(double elapsed, DateUtil.TimeUnit minUnits)
elapsed - Elapsed time in ms.minUnits - Minimum units for return value (null = ms).public static String formatElapsed(double elapsed, boolean pluralize, boolean abbreviated, boolean round)
elapsed - Elapsed time in ms.pluralize - If true, pluralize units when appropriate.abbreviated - If true, use abbreviated form of units.round - If true, round result to an integer.public static String formatElapsed(double elapsed, boolean pluralize, boolean abbreviated, boolean round, DateUtil.TimeUnit minUnits)
elapsed - Elapsed time in ms.pluralize - If true, pluralize units when appropriate.abbreviated - If true, use abbreviated form of units.round - If true, round result to an integer.minUnits - Minimum units for return value (null = ms).public static double parseElapsed(String value)
value - The string value to parse.public static double parseElapsed(String value, DateUtil.TimeUnit units)
value - The string value to parse.units - The units of the returned value (defaults to ms).public static String formatDuration(long duration)
duration - Duration in ms.public static String formatDuration(long duration, DateUtil.TimeUnit accuracy)
duration - Duration in ms.accuracy - Accuracy of output.public static String formatDuration(long duration, DateUtil.TimeUnit accuracy, boolean pluralize, boolean abbreviated)
duration - Duration in ms.accuracy - Accuracy of output.pluralize - If true, pluralize units when appropriate.abbreviated - If true, use abbreviated form of units.public static TimeZone getLocalTimeZone()
public static String formatAge(Date dob)
Returns age as a formatted string expressed in days, months, or years, depending on whether person is an infant (< 2 mos), toddler (> 2 mos, < 2 yrs), or more than 2 years old.
dob - Date of person's birthpublic static String formatAge(Date dob, boolean pluralize, Date refDate)
Returns age as a formatted string expressed in days, months, or years, depending on whether person is an infant (< 2 mos), toddler (> 2 mos, < 2 yrs), or more than 2 years old.
Allows the caller to specify an "as-of" date. The calculated age will be as-of the provided date, rather than as-of the current date.
Allows the caller to specify whether or not to pluralize the age units in the age display string.
dob - Date of person's birthpluralize - If true, pluralize the age units in the age display string.refDate - The date as of which to calculate the Person's age (null means today).public static Date toDate(int day, int month, int year)
day - Day of month.month - Month (1=January, etc.)year - Year (4 digit).public static Date toDate(int day, int month, int year, int hr, int min, int sec)
day - Day of month.month - Month (1=January, etc.)year - Year (4 digit).hr - Hour of day.min - Minutes past the hour.sec - Seconds past the minute.Copyright © 2017 Regenstrief Center for Biomedical Informatics. All rights reserved.