DateUtil

public final class DateUtil

Utility class for various classes from org.joda.time package. Using this class for retrieving the current date and time will allow mocking time, so should be always used throughout the platform instead of calling the underlying date-time API directly.

Methods

daysPast

public static int daysPast(LocalDate localDate, DayOfWeek dayOfWeek)

Counts the days passed between given date and given day of week.

Parameters:
  • localDate – the given date
  • dayOfWeek – the given day of week
Returns:

the number of days passed between localDate and a org.motechproject.commons.date.model.DayOfWeek

daysStarting

public static List<DayOfWeek> daysStarting(DayOfWeek day, int numberOfDays)

Returns list of days equal to numberOfDays from given day.

Parameters:
  • day – the day of week from which list should begin
  • numberOfDays – the number of days which should be included in the list
Returns:

the list of days

daysToCalendarWeekEnd

public static int daysToCalendarWeekEnd(LocalDate date, int calendarWeekStartDay)

Returns number of days left until weekend.

Parameters:
  • date – the date from which days should be counted
  • calendarWeekStartDay – the day at which weekend starts
Returns:

days left until weekend

endOfDay

public static DateTime endOfDay(Date dateTime)

Creates DateTime with time set to 23:59:59:999 and date equal to the given one.

Parameters:
  • dateTime – the Date to be stored
Returns:

the new DateTime instance

getDifferenceOfDatesInYears

public static int getDifferenceOfDatesInYears(Date startDate)

Returns difference, in years, between given date and today.

Parameters:
  • startDate – the date from which year are counted
Returns:

difference in years

greaterThanOrEqualTo

public static List<DateTime> greaterThanOrEqualTo(DateTime date, List<DateTime> dates)

Filters given dates and returns only those that are past or at the given date.

Parameters:
  • date – the date to be used as filter
  • dates – dates to be filtered
Returns:

list of date that are equal or greater than given date

inRange

public static boolean inRange(DateTime reference, DateTime start, DateTime end)

Checks if first date is in period between second and third.

Parameters:
  • reference – the date to be checked
  • start – the start of the period
  • end – the end of the period
Returns:

true if first date is in range, false otherwise

isOnOrAfter

public static boolean isOnOrAfter(DateTime firstDate, DateTime secondDate)

Checks whether first date is on or after second one.

Parameters:
  • firstDate – the date what should be on or after
  • secondDate – the date to compare to
Returns:

false if first date is before second, true otherwise

isOnOrBefore

public static boolean isOnOrBefore(DateTime firstDate, DateTime secondDate)

Checks whether first date is on or before second one.

Parameters:
  • firstDate – the date what should be on or before
  • secondDate – the date to compare to
Returns:

false if first date is after second, true otherwise

lessThan

public static List<DateTime> lessThan(DateTime date, List<DateTime> dates)

Filters given dates and returns only those that are before the given date.

Parameters:
  • date – the date to be used as filter
  • dates – dates to be filtered
Returns:

list of date that are before the given date

newDate

public static LocalDate newDate(int year, int month, int day)

Creates new instance of LocalDate.

Parameters:
  • year – the year to be stored in created instance
  • month – the month to be stored in created instance
  • day – the day to be stored in created instance
Returns:

the instance of LocalDate with given year, month and day

newDate

public static LocalDate newDate(Date date)

Creates new LocalDate from given Date.

Parameters:
  • date – the Date to be parsed to LocalDate
Returns:

the new instance of LocalTime, null if date was null

newDate

public static LocalDate newDate(DateTime dateTime)

Creates new LocalDate from given DateTime.

Parameters:
  • dateTime – the DateTime to be parsed to LocalDate
Returns:

the new instance of LocalTime, null if date was null

newDateTime

public static DateTime newDateTime(LocalDate localDate, int hour, int minute, int second)

Creates new instance of DateTime.

Parameters:
  • localDate – the date to be stored in created instance
  • hour – the hour to be stored in created instance
  • minute – the minute to be stored in created instance
  • second – the second to be stored in created instance
Returns:

the instance of DateTime with given date and time

newDateTime

public static DateTime newDateTime(Date date)

Creates new DateTime from given Date.

Parameters:
  • date – the Date to be parsed to DateTime
Returns:

the new DateTime instance

newDateTime

public static DateTime newDateTime(LocalDate date)

Creates new DateTime from given LocalDate. Time is set to 00:00:00.

Parameters:
  • date – the Date to be parsed to DateTime
Returns:

the new DateTime instance

newDateTime

public static DateTime newDateTime(LocalDate localDate, Time time)

Creates new DateTime from given LocalDate and Time.

Parameters:
  • localDate – the LocalDate to be stored
  • time – the Time to be stored
Returns:

the new DateTime instance

newDateTime

public static DateTime newDateTime(int year, int month, int day, Time time)

Creates new DateTime from given information.

Parameters:
  • year – the year to be stored
  • month – the month to be stored
  • day – the day to be stored
  • time – the time to be stored
Returns:

the new DateTime instance

newDateTime

public static DateTime newDateTime(int year, int month, int day)

Creates new DateTime from given information. Time is set to 00:00:00.

Parameters:
  • year – the year to be stored
  • month – the month to be stored
  • day – the day to be stored
Returns:

the new DateTime instance

newDateTime

public static DateTime newDateTime(int year, int month, int day, int hour, int minute, int second)

Creates new DateTime from given information.

Parameters:
  • year – the year to be stored
  • month – the month to be stored
  • day – the day to be stored
  • hour – the hour to be stored
  • minute – the minute to be stored
  • second – the second to be stored
Returns:

the new DateTime instance

nextApplicableWeekDay

public static DateTime nextApplicableWeekDay(DateTime fromDate, List<DayOfWeek> applicableDays)

Returns first next applicable week day.

Parameters:
  • fromDate – the date from which next day should be searched
  • applicableDays – list of applicable days
Returns:

next applicable week day

nextApplicableWeekDayIncludingFromDate

public static DateTime nextApplicableWeekDayIncludingFromDate(DateTime fromDate, List<DayOfWeek> applicableDays)

Returns first next applicable week day(including current day).

Parameters:
  • fromDate – the date from which next day should be searched
  • applicableDays – list of applicable days
Returns:

next applicable week day

now

public static DateTime now()

Returns current time as an instance of DateTime.

Returns:the current time

nowUTC

public static DateTime nowUTC()

Returns current time in UTC time zone.

Returns:the current time as instance of DateTime

setTimeZone

public static DateTime setTimeZone(DateTime dateTime)

Sets time zone, for given DateTime, to default.

Parameters:
  • dateTime – the DateTime to have time zone set
Returns:

the DateTime with time zone set

setTimeZoneUTC

public static DateTime setTimeZoneUTC(DateTime dateTime)

Sets time zone, for given DateTime, to UTC time zone.

Parameters:
  • dateTime – the DateTime to have time zone set
Returns:

the DateTime with time zone set

time

public static Time time(DateTime dateTime)

Extracts time from given DateTime and converts it to an instance of Time.

Parameters:
  • dateTime – the DateTime storing the time
Returns:

the new instance of Time

today

public static LocalDate today()

Returns current local date.

Returns:the current local date as an instance of LocalDate

tomorrow

public static LocalDate tomorrow()

Returns tomorrow local date.

Returns:the tomorrow local date as an instance of LocalDate