Package io.mosip.kernel.core.util
Class DateUtils
- java.lang.Object
-
- io.mosip.kernel.core.util.DateUtils
-
public final class DateUtils extends Object
Utilities for Date Time operations. Provide Date and Time utility for usage across the application to manipulate dates or calendars- Since:
- 1.0.0
- Author:
- Ravi C Balaji, Bal Vikash Sharma
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DateaddDays(Date date, int days)Adds a number of days to a date returning a new Date object.static DateaddHours(Date date, int hours)Adds a number of hours to a date returning a new Date object.static DateaddMinutes(Date date, int minutes)Adds a number of minutes to a date returning a new Date object.static DateaddSeconds(Date date, int seconds)Adds a number of seconds to a date returning a new Date object.static booleanafter(LocalDateTime d1, LocalDateTime d2)Tests if this java.time.LocalDateTime is after the specified java.time.LocalDateTime.static booleanafter(Date d1, Date d2)Tests if this date is after the specified date.static booleanbefore(LocalDateTime d1, LocalDateTime d2)Tests if this LocalDateTime is before the specified LocalDateTime.static booleanbefore(Date d1, Date d2)Tests if this date is before the specified date.static LocalDateTimeconvertUTCToLocalDateTime(String utcDateTime)Converts UTC string to java.time.LocalDateTime ignoring zone offset.static StringformatCalendar(Calendar calendar, String pattern)Formats a calendar into a specific pattern.static StringformatCalendar(Calendar calendar, String pattern, Locale locale)Formats a calendar into a specific pattern in a time zone and locale.static StringformatCalendar(Calendar calendar, String pattern, TimeZone timeZone)Formats a calendar into a specific pattern in a time zone.static StringformatCalendar(Calendar calendar, String pattern, TimeZone timeZone, Locale locale)Formats a calendar into a specific pattern in a time zone and locale.static StringformatDate(Date date, String pattern)Formats a date/time into a specific pattern.static StringformatDate(Date date, String pattern, TimeZone timeZone)Formats a date/time into a specific pattern in a time zone.static StringformatDate(Date date, String pattern, TimeZone timeZone, Locale locale)Formats a date/time into a specific pattern in a time zone and locale.static StringformatToISOString(LocalDateTime localDateTime)Formats java.time.LocalDateTime to UTC string in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSS'Z' ignoring zone offset.static StringgetCurrentDateTimeString()Provides current DateTime string with system zone offset and in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSSXXX.static LocalDateTimegetUTCCurrentDateTime()Provides current UTC java.time.LocalDateTime.static StringgetUTCCurrentDateTimeString()Provides UTC Current DateTime string in default ISO pattern.static StringgetUTCCurrentDateTimeString(String pattern)Provides UTC Current DateTime string in given pattern.static StringgetUTCTimeFromDate(Date date)This method to convert “java.util.Date” time stamp to UTC date stringstatic booleanisSameDay(LocalDateTime d1, LocalDateTime d2)Checks if two java.time.LocalDateTime objects are on the same day ignoring time.static booleanisSameDay(Date d1, Date d2)Checks if two date objects are on the same day ignoring time.static booleanisSameInstant(LocalDateTime d1, LocalDateTime d2)Checks if two java.time.LocalDateTime objects represent the same instant in time.static booleanisSameInstant(Date d1, Date d2)Checks if two date objects represent the same instant in time.static LocalDateTimeparseDateToLocalDateTime(Date date)Parses Date to java.time.LocalDateTime adjusted for system time zone.static DateparseToDate(String dateString, String pattern)Parses date string of given pattern to java.util.Date.static DateparseToDate(String dateTime, String pattern, TimeZone timeZone)Parses date string of given pattern and TimeZone to java.util.Date.static LocalDateTimeparseToLocalDateTime(String dateTime)Parses UTC string of pattern yyyy-MM-dd'T'HH:mm:ss.SSS or yyyy-MM-dd'T'HH:mm:ss.SSS'Z' to java.time.LocalDateTime.static DateparseUTCToDate(String utcDateTime)Parses given UTC string of ISO pattern yyyy-MM-dd'T'HH:mm:ss.SSS'Z' to java.util.Date.static DateparseUTCToDate(String utcDateTime, String pattern)Parses UTC string of given pattern to java.util.Date.static LocalDateTimeparseUTCToLocalDateTime(String utcDateTime)Parses UTC string to java.time.LocalDateTime adjusted for system time zone.static LocalDateTimeparseUTCToLocalDateTime(String utcDateTime, String pattern)Parses UTC string of given pattern to java.time.LocalDateTime.static StringtoISOString(LocalDateTime localDateTime)Converts java.time.LocalDateTime to UTC string in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.static StringtoISOString(Date date)Converts java.util.Date to UTC string in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.
-
-
-
Method Detail
-
addDays
public static Date addDays(Date date, int days)
Adds a number of days to a date returning a new Date object.
- Parameters:
date- the date, not nulldays- the number of days to add, may be negative- Returns:
- the new Date with the number of days added
- Throws:
IllegalArgumentException- if the date is null
-
addHours
public static Date addHours(Date date, int hours)
Adds a number of hours to a date returning a new Date object.
- Parameters:
date- the date, not nullhours- the hours to add, may be negative- Returns:
- the new Date with the hours added
- Throws:
IllegalArgumentException- if the date is null
-
addMinutes
public static Date addMinutes(Date date, int minutes)
Adds a number of minutes to a date returning a new Date object.
- Parameters:
date- the date, not nullminutes- the minutes to add, may be negative- Returns:
- the new Date with the minutes added
- Throws:
IllegalArgumentException- if the date is null
-
addSeconds
public static Date addSeconds(Date date, int seconds)
Adds a number of seconds to a date returning a new Date object.
- Parameters:
date- the date, not nullseconds- the seconds to add, may be negative- Returns:
- the new Date with the seconds added
- Throws:
IllegalArgumentException- if the date is null
-
formatDate
public static String formatDate(Date date, String pattern)
Formats a date/time into a specific pattern.
- Parameters:
date- the date to format, not nullpattern- the pattern to use to format the date, not null- Returns:
- the formatted date
- Throws:
IllegalArgumentException- if the date/pattern is null
-
formatDate
public static String formatDate(Date date, String pattern, TimeZone timeZone)
Formats a date/time into a specific pattern in a time zone.
- Parameters:
date- the date to format, not nullpattern- the pattern to use to format the date, not nulltimeZone- the time zone to use, may be null- Returns:
- the formatted date
- Throws:
IllegalArgumentException- if the date/pattern/timeZone is null
-
formatDate
public static String formatDate(Date date, String pattern, TimeZone timeZone, Locale locale)
Formats a date/time into a specific pattern in a time zone and locale.
- Parameters:
date- , the date to format, not nullpattern- , the pattern to use to format the date, not nulltimeZone- , the time zone to use, may be nulllocale- , the locale to use, may be null- Returns:
- the formatted date
- Throws:
IllegalArgumentException- if the date/pattern/timeZone is null
-
formatCalendar
public static String formatCalendar(Calendar calendar, String pattern)
Formats a calendar into a specific pattern.- Parameters:
calendar- , the calendar to format, not nullpattern- , the pattern to use to format the calendar, not null- Returns:
- the formatted calendar
- Throws:
IllegalArgumentException- if the calendar/pattern is null
-
formatCalendar
public static String formatCalendar(Calendar calendar, String pattern, TimeZone timeZone)
Formats a calendar into a specific pattern in a time zone.- Parameters:
calendar- , the calendar to format, not nullpattern- , the pattern to use to format the calendar, not nulltimeZone- , the time zone to use, may be null- Returns:
- the formatted calendar
- Throws:
IllegalArgumentException- if the calendar/pattern/timeZone is null
-
formatCalendar
public static String formatCalendar(Calendar calendar, String pattern, Locale locale)
Formats a calendar into a specific pattern in a time zone and locale.- Parameters:
calendar- , the calendar to format, not nullpattern- , the pattern to use to format the calendar, not nulllocale- , the locale to use, may be null- Returns:
- the formatted calendar
- Throws:
IllegalArgumentException- if the calendar/pattern/locale is null
-
formatCalendar
public static String formatCalendar(Calendar calendar, String pattern, TimeZone timeZone, Locale locale)
Formats a calendar into a specific pattern in a time zone and locale.- Parameters:
calendar- , the calendar to format, not nullpattern- , the pattern to use to format the calendar, not nulltimeZone- , the time zone to use, may be nulllocale- , the locale to use, may be null- Returns:
- the formatted calendar
- Throws:
IllegalArgumentException- if the calendar/pattern/timeZone is null
-
after
public static boolean after(Date d1, Date d2)
Tests if this date is after the specified date.- Parameters:
d1- a Date by which we will compared2- a Date with which we want to compare- Returns:
- true if and only if the instant represented by d1 Date object is strictly later than the instant represented by d2; false otherwise.
- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,Date
-
before
public static boolean before(Date d1, Date d2)
Tests if this date is before the specified date.- Parameters:
d1- a Date by which we will compared2- a Date with which we want to compare- Returns:
- true if and only if the instant of time represented by d1 Date object is strictly earlier than the instant represented by d2; false otherwise.
- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,Date
-
isSameDay
public static boolean isSameDay(Date d1, Date d2)
Checks if two date objects are on the same day ignoring time.
28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.
28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.- Parameters:
d1- a Date by which we will compared2- a Date with which we want to compare- Returns:
- true if they represent the same day
- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,Date
-
isSameInstant
public static boolean isSameInstant(Date d1, Date d2)
Checks if two date objects represent the same instant in time.
This method compares the long millisecond time of the two objects.- Parameters:
d1- a Date by which we will compared2- a Date with which we want compare- Returns:
trueif they represent the same millisecond instant- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,Date
-
after
public static boolean after(LocalDateTime d1, LocalDateTime d2)
Tests if this java.time.LocalDateTime is after the specified java.time.LocalDateTime.- Parameters:
d1- a LocalDateTime which is going to be comparedd2- a LocalDateTime by which we will compare- Returns:
- true if and only if the instant represented by d1 LocalDateTime object is strictly later than the instant represented by d2; false otherwise.
- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,LocalDateTime
-
before
public static boolean before(LocalDateTime d1, LocalDateTime d2)
Tests if this LocalDateTime is before the specified LocalDateTime.- Parameters:
d1- a LocalDateTime which is going to be comparedd2- a LocalDateTime by which we will compare- Returns:
- true if and only if the instant of time represented by d1 LocalDateTime object is strictly earlier than the instant represented by d2; false otherwise.
- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,LocalDateTime
-
isSameDay
public static boolean isSameDay(LocalDateTime d1, LocalDateTime d2)
Checks if two java.time.LocalDateTime objects are on the same day ignoring time.
28 Mar 2002 13:45 and 28 Mar 2002 06:01 would return true.
28 Mar 2002 13:45 and 12 Mar 2002 13:45 would return false.- Parameters:
d1- a LocalDateTime which is going to be comparedd2- a LocalDateTime by which we will compare- Returns:
- true if they represent the same day
- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,LocalDateTime
-
isSameInstant
public static boolean isSameInstant(LocalDateTime d1, LocalDateTime d2)
Checks if two java.time.LocalDateTime objects represent the same instant in time.
This method compares the long millisecond time of the two objects.- Parameters:
d1- a LocalDateTime which is going to be comparedd2- a LocalDateTime by which we will compare- Returns:
- true if they represent the same millisecond instant
- Throws:
IllegalArgumentException- if thed1,d2is null- See Also:
IllegalArgumentException,LocalDateTime
-
toISOString
public static String toISOString(LocalDateTime localDateTime)
Converts java.time.LocalDateTime to UTC string in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.- Parameters:
localDateTime- java.time.LocalDateTime- Returns:
- a date String
-
toISOString
public static String toISOString(Date date)
Converts java.util.Date to UTC string in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.- Parameters:
date- java.util.Date- Returns:
- a date String
-
formatToISOString
public static String formatToISOString(LocalDateTime localDateTime)
Formats java.time.LocalDateTime to UTC string in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSS'Z' ignoring zone offset.- Parameters:
localDateTime- java.time.LocalDateTime- Returns:
- a date String
-
getUTCCurrentDateTime
public static LocalDateTime getUTCCurrentDateTime()
Provides current UTC java.time.LocalDateTime.- Returns:
- LocalDateTime
- See Also:
LocalDateTime
-
getUTCCurrentDateTimeString
public static String getUTCCurrentDateTimeString()
Provides UTC Current DateTime string in default ISO pattern. Obtains the current date-time from the system clock in the default time-zone.This will query the
system clockin the default time-zone to obtain the current date-time. The offset will be calculated from the time-zone in the clock.Using this method will prevent the ability to use an alternate clock for testing because the clock is hard-coded.
- Returns:
- the current date-time using the system clock, not null
-
getUTCCurrentDateTimeString
public static String getUTCCurrentDateTimeString(String pattern)
Provides UTC Current DateTime string in given pattern.- Parameters:
pattern- is of type String- Returns:
- date String
-
getCurrentDateTimeString
public static String getCurrentDateTimeString()
Provides current DateTime string with system zone offset and in default ISO pattern - yyyy-MM-dd'T'HH:mm:ss.SSSXXX.- Returns:
- a date String
-
convertUTCToLocalDateTime
public static LocalDateTime convertUTCToLocalDateTime(String utcDateTime)
Converts UTC string to java.time.LocalDateTime ignoring zone offset.- Parameters:
utcDateTime- is of type String- Returns:
- a LocalDateTime
- Throws:
DateTimeParseException- if not able to parse the utcDateTime string for the pattern.- See Also:
LocalDateTime
-
parseUTCToLocalDateTime
public static LocalDateTime parseUTCToLocalDateTime(String utcDateTime)
Parses UTC string to java.time.LocalDateTime adjusted for system time zone.- Parameters:
utcDateTime- is of type String- Returns:
- a LocalDateTime
- See Also:
LocalDateTime
-
parseToLocalDateTime
public static LocalDateTime parseToLocalDateTime(String dateTime)
Parses UTC string of pattern yyyy-MM-dd'T'HH:mm:ss.SSS or yyyy-MM-dd'T'HH:mm:ss.SSS'Z' to java.time.LocalDateTime.- Parameters:
dateTime- is of type String- Returns:
- a LocalDateTime
- Throws:
DateTimeParseException- if not able to parse the utcDateTime string for the pattern- See Also:
LocalDateTime
-
parseUTCToLocalDateTime
public static LocalDateTime parseUTCToLocalDateTime(String utcDateTime, String pattern)
Parses UTC string of given pattern to java.time.LocalDateTime.- Parameters:
utcDateTime- is of type Stringpattern- is of type String- Returns:
- LocalDateTime
- Throws:
ParseException- if not able to parse the utcDateTime string for the pattern.- See Also:
ParseException,LocalDateTime
-
parseDateToLocalDateTime
public static LocalDateTime parseDateToLocalDateTime(Date date)
Parses Date to java.time.LocalDateTime adjusted for system time zone.- Parameters:
date- is of type String- Returns:
- a LocalDateTime
- See Also:
LocalDateTime
-
parseUTCToDate
public static Date parseUTCToDate(String utcDateTime)
Parses given UTC string of ISO pattern yyyy-MM-dd'T'HH:mm:ss.SSS'Z' to java.util.Date.- Parameters:
utcDateTime- is of type String- Returns:
- a Date
- Throws:
ParseException- if not able to parse theutcDateTimestring in given Default utcDateTime pattern - yyyy-MM-dd'T'HH:mm:ss.SSS'Z'.- See Also:
ParseException
-
parseUTCToDate
public static Date parseUTCToDate(String utcDateTime, String pattern)
Parses UTC string of given pattern to java.util.Date.- Parameters:
utcDateTime- is of type Stringpattern- is of type String- Returns:
- a Date
- Throws:
ParseException- if not able to parse the dateTime string in given string pattern.- See Also:
ParseException
-
parseToDate
public static Date parseToDate(String dateTime, String pattern, TimeZone timeZone)
Parses date string of given pattern and TimeZone to java.util.Date.- Parameters:
dateTime- is of type Stringpattern- is of type StringtimeZone- is of type java.util.TimeZone- Returns:
- a Date
- Throws:
ParseException- if not able to parse the dateTime string in given string pattern.- See Also:
ParseException,TimeZone
-
parseToDate
public static Date parseToDate(String dateString, String pattern)
Parses date string of given pattern to java.util.Date.- Parameters:
dateString- The date string.pattern- The date format pattern which should respect the SimpleDateFormat rules.- Returns:
- The parsed date object.
- Throws:
ParseException- If the given date string or its actual date is invalid based on the given date format pattern.NullPointerException- IfdateStringordateFormatis null.- See Also:
SimpleDateFormat
-
-