com.jdroid.java.utils
Class DateUtils

java.lang.Object
  extended by com.jdroid.java.utils.DateUtils

public abstract class DateUtils
extends Object

Utilities for Dates and Calendars


Nested Class Summary
static class DateUtils.DayOfWeek
           
 
Field Summary
static String E_DATE_FORMAT
          Date format like Fri
static String EEEE_DATE_FORMAT
          Date format like Friday
static String EEEE_MMMD_DATE_FORMAT
          Date format like Monday Nov 5
static String EEEE_MMMDYYYY_DATE_FORMAT
          Date format like Monday Nov 5 1985
static String EEEEDMMMM_DATE_FORMAT
          Date format like Friday 5 November
static String EEEEMMMM_DATE_FORMAT
          Date format like Friday November
static String EEMMMDYYYY_DATE_FORMAT
          Date format like Sat, Dec 15, 2012
static String EHHMMAA_DATE_FORMAT
          Date format like Fri 5:15 AM
static String HHMMAA_DATE_FORMAT
          Date format like 03:45 PM
static long MILLIS_PER_DAY
          Number of milliseconds in a standard day.
static long MILLIS_PER_HOUR
          Number of milliseconds in a standard hour.
static long MILLIS_PER_MINUTE
          Number of milliseconds in a standard minute.
static long MILLIS_PER_SECOND
          Number of milliseconds in a standard second.
static String MMDDYYYY_DATE_FORMAT
          Date format like 10/25/2010
static String MMDDYYYY_SLASH_DATE_FORMAT
          Date format like 10-25-2010
static String MMDDYYYYHHMM_DATE_FORMAT
          Date format like 10/25/2010 21:30
static String MMMD_DATE_FORMAT
          Date format like Nov 5
static String MMMDHHMMAA_DATE_FORMAT
          Date format like Nov 5 3:45 PM
static String MMMDYYYY_DATE_FORMAT
          Date format like Nov 5 1985
static String MMMDYYYYHHMMAA_DATE_FORMAT
          Date format like Nov 5 1985 3:45 PM
static String MMMMD_DATE_FORMAT
          Date format like November 5
static int SECONDS_PER_DAY
          Seconds in a day
static int SECONDS_PER_HOUR
          Seconds in an hour
static int SECONDS_PER_MINUTE
          Seconds in a minute
static int WEEK
          Seconds in a week
static String YYYYMMDD_DATE_FORMAT
          Date format like 2010-10-25
static String YYYYMMDDHHMMSS_DATE_FORMAT
          Date format like 2010-10-25 21:30:00
static String YYYYMMDDHHMMSSZ_DATE_FORMAT
          Date format like yyyy-MM-dd HH:mm:ss Z
static String YYYYMMDDTHHMMSSZ_DATE_FORMAT
          Date format like yyyy-MM-ddTHH:mm:ss Z
 
Constructor Summary
DateUtils()
           
 
Method Summary
static Date addDays(Date date, int days)
           
static Date addMonths(Date date, int days)
           
static Date addSeconds(Date date, int seconds)
           
static Date addYears(Date date, int years)
           
static boolean containsPeriod(Date startDate1, Date endDate1, Date startDate2, Date endDate2)
          Returns true if the first period contains the second periods
static String format(Date date, DateFormat dateFormat)
          Transform the Date to a String using the received SimpleDateFormat
static String format(Date date, String dateFormat)
           
static String formatDate(Date date)
           
static String formatDuration(long duration)
           
static String formatTime(Date date)
           
static String formatToCardinal(Date date)
          Transform the Date to a String with a format like Friday November 5th
static Date getDate(Date date, Date time)
           
static Date getDate(int year, int monthOfYear, int dayOfMonth)
          Creates a Date for the specified day
static Date getDate(long milliseconds)
           
static int getDay(Date date)
           
static int getHour(Date date, Boolean is24Hour)
           
static int getHour(Date date, TimeZone timeZone, Boolean is24Hour)
           
static Date getLastDayOfMonth(Date date)
           
static int getMinute(Date date)
           
static int getMinute(Date date, TimeZone timeZone)
           
static int getMonth(Date date)
           
static Date getTime(int hour, int minutes)
           
static Date getTime(int hour, int minutes, Boolean is24Hour)
           
static DateUtils.DayOfWeek getWeekDay(Date date)
           
static int getYear()
           
static int getYear(Date date)
           
static void init()
           
static boolean isAfter(Date date, Date dateToCompare)
          Tests if the date is after than the specified dateToCompare.
static boolean isAfterEquals(Date date, Date dateToCompare)
          Tests if the date is after or equals than the specified dateToCompare.
static boolean isBefore(Date date, Date dateToCompare)
          Tests if the date is before than the specified dateToCompare.
static boolean isBeforeEquals(Date date, Date dateToCompare)
          Tests if the date is before or equals than the specified dateToCompare.
static boolean isBetween(Date date, Date startDate, Date endDate)
           
static Date monthsAway(int months)
           
static Date now()
           
static Date oneMonthInFuture()
           
static Date oneMonthInPast()
           
static Date parse(String dateFormatted, SimpleDateFormat dateFormat)
           
static Date parse(String dateFormatted, String dateFormat)
           
static boolean periodsOverlap(Date startDate1, Date endDate1, Date startDate2, Date endDate2)
          Returns true if two periods overlap
static Date today()
           
static Date tomorrow()
           
static void truncate(Calendar calendar)
          Truncate the Calendar removing hours, minutes, seconds and milliseconds
static Date truncate(Date date)
          Truncate the date removing hours, minutes, seconds and milliseconds
static Date yesterday()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SECONDS_PER_MINUTE

public static final int SECONDS_PER_MINUTE
Seconds in a minute

See Also:
Constant Field Values

SECONDS_PER_HOUR

public static final int SECONDS_PER_HOUR
Seconds in an hour

See Also:
Constant Field Values

SECONDS_PER_DAY

public static final int SECONDS_PER_DAY
Seconds in a day

See Also:
Constant Field Values

WEEK

public static final int WEEK
Seconds in a week

See Also:
Constant Field Values

MILLIS_PER_SECOND

public static final long MILLIS_PER_SECOND
Number of milliseconds in a standard second.

See Also:
Constant Field Values

MILLIS_PER_MINUTE

public static final long MILLIS_PER_MINUTE
Number of milliseconds in a standard minute.

See Also:
Constant Field Values

MILLIS_PER_HOUR

public static final long MILLIS_PER_HOUR
Number of milliseconds in a standard hour.

See Also:
Constant Field Values

MILLIS_PER_DAY

public static final long MILLIS_PER_DAY
Number of milliseconds in a standard day.

See Also:
Constant Field Values

YYYYMMDDTHHMMSSZ_DATE_FORMAT

public static final String YYYYMMDDTHHMMSSZ_DATE_FORMAT
Date format like yyyy-MM-ddTHH:mm:ss Z

See Also:
Constant Field Values

YYYYMMDDHHMMSSZ_DATE_FORMAT

public static final String YYYYMMDDHHMMSSZ_DATE_FORMAT
Date format like yyyy-MM-dd HH:mm:ss Z

See Also:
Constant Field Values

YYYYMMDDHHMMSS_DATE_FORMAT

public static final String YYYYMMDDHHMMSS_DATE_FORMAT
Date format like 2010-10-25 21:30:00

See Also:
Constant Field Values

MMDDYYYYHHMM_DATE_FORMAT

public static final String MMDDYYYYHHMM_DATE_FORMAT
Date format like 10/25/2010 21:30

See Also:
Constant Field Values

MMDDYYYY_DATE_FORMAT

public static final String MMDDYYYY_DATE_FORMAT
Date format like 10/25/2010

See Also:
Constant Field Values

MMDDYYYY_SLASH_DATE_FORMAT

public static final String MMDDYYYY_SLASH_DATE_FORMAT
Date format like 10-25-2010

See Also:
Constant Field Values

YYYYMMDD_DATE_FORMAT

public static final String YYYYMMDD_DATE_FORMAT
Date format like 2010-10-25

See Also:
Constant Field Values

EHHMMAA_DATE_FORMAT

public static final String EHHMMAA_DATE_FORMAT
Date format like Fri 5:15 AM

See Also:
Constant Field Values

E_DATE_FORMAT

public static final String E_DATE_FORMAT
Date format like Fri

See Also:
Constant Field Values

EEEE_DATE_FORMAT

public static final String EEEE_DATE_FORMAT
Date format like Friday

See Also:
Constant Field Values

MMMDHHMMAA_DATE_FORMAT

public static final String MMMDHHMMAA_DATE_FORMAT
Date format like Nov 5 3:45 PM

See Also:
Constant Field Values

MMMDYYYYHHMMAA_DATE_FORMAT

public static final String MMMDYYYYHHMMAA_DATE_FORMAT
Date format like Nov 5 1985 3:45 PM

See Also:
Constant Field Values

MMMDYYYY_DATE_FORMAT

public static final String MMMDYYYY_DATE_FORMAT
Date format like Nov 5 1985

See Also:
Constant Field Values

EEEE_MMMDYYYY_DATE_FORMAT

public static final String EEEE_MMMDYYYY_DATE_FORMAT
Date format like Monday Nov 5 1985

See Also:
Constant Field Values

MMMD_DATE_FORMAT

public static final String MMMD_DATE_FORMAT
Date format like Nov 5

See Also:
Constant Field Values

EEEE_MMMD_DATE_FORMAT

public static final String EEEE_MMMD_DATE_FORMAT
Date format like Monday Nov 5

See Also:
Constant Field Values

MMMMD_DATE_FORMAT

public static final String MMMMD_DATE_FORMAT
Date format like November 5

See Also:
Constant Field Values

HHMMAA_DATE_FORMAT

public static final String HHMMAA_DATE_FORMAT
Date format like 03:45 PM

See Also:
Constant Field Values

EEEEDMMMM_DATE_FORMAT

public static final String EEEEDMMMM_DATE_FORMAT
Date format like Friday 5 November

See Also:
Constant Field Values

EEEEMMMM_DATE_FORMAT

public static final String EEEEMMMM_DATE_FORMAT
Date format like Friday November

See Also:
Constant Field Values

EEMMMDYYYY_DATE_FORMAT

public static final String EEMMMDYYYY_DATE_FORMAT
Date format like Sat, Dec 15, 2012

See Also:
Constant Field Values
Constructor Detail

DateUtils

public DateUtils()
Method Detail

init

public static final void init()

parse

public static Date parse(String dateFormatted,
                         String dateFormat)
Parameters:
dateFormatted - The formatted string to parse
dateFormat -
Returns:
A date that represents the formatted string

parse

public static Date parse(String dateFormatted,
                         SimpleDateFormat dateFormat)
Parameters:
dateFormatted - The formatted string to parse
dateFormat -
Returns:
A date that represents the formatted string

formatToCardinal

public static String formatToCardinal(Date date)
Transform the Date to a String with a format like Friday November 5th

Parameters:
date - The Date to be formatted
Returns:
A String that represent the date with the pattern

format

public static String format(Date date,
                            String dateFormat)
Parameters:
date - The Date to be formatted
dateFormat - The DateFormat used to format the Date
Returns:
A String that represent the date with the pattern

format

public static String format(Date date,
                            DateFormat dateFormat)
Transform the Date to a String using the received SimpleDateFormat

Parameters:
date - The Date to be formatted
dateFormat - The DateFormat used to format the Date
Returns:
A String that represent the date with the pattern

formatDate

public static String formatDate(Date date)

formatTime

public static String formatTime(Date date)

getDate

public static Date getDate(int year,
                           int monthOfYear,
                           int dayOfMonth)
Creates a Date for the specified day

Parameters:
year - The year
monthOfYear - The month number (starting on 0)
dayOfMonth - The day of the month
Returns:
The Date

getDate

public static Date getDate(Date date,
                           Date time)

getDate

public static Date getDate(long milliseconds)

getTime

public static Date getTime(int hour,
                           int minutes)

getTime

public static Date getTime(int hour,
                           int minutes,
                           Boolean is24Hour)

getYear

public static int getYear()

getYear

public static int getYear(Date date)

getMonth

public static int getMonth(Date date)

getDay

public static int getDay(Date date)

getHour

public static int getHour(Date date,
                          Boolean is24Hour)

getHour

public static int getHour(Date date,
                          TimeZone timeZone,
                          Boolean is24Hour)

getMinute

public static int getMinute(Date date)

getMinute

public static int getMinute(Date date,
                            TimeZone timeZone)

getWeekDay

public static DateUtils.DayOfWeek getWeekDay(Date date)

addSeconds

public static Date addSeconds(Date date,
                              int seconds)

addDays

public static Date addDays(Date date,
                           int days)

addMonths

public static Date addMonths(Date date,
                             int days)

addYears

public static Date addYears(Date date,
                            int years)

truncate

public static Date truncate(Date date)
Truncate the date removing hours, minutes, seconds and milliseconds

Parameters:
date - The Date to truncate
Returns:
The truncated Date

truncate

public static void truncate(Calendar calendar)
Truncate the Calendar removing hours, minutes, seconds and milliseconds

Parameters:
calendar - The Calendar to truncate

now

public static Date now()
Returns:
the current moment

isBetween

public static boolean isBetween(Date date,
                                Date startDate,
                                Date endDate)
Parameters:
date - The date to compare
startDate - The left between' side
endDate - The right between's side
Returns:
true if the date is in the middle of startDate and endDate

isBefore

public static boolean isBefore(Date date,
                               Date dateToCompare)
Tests if the date is before than the specified dateToCompare.

Parameters:
date - the date to compare with the dateToCompare.
dateToCompare - the date to compare with the date.
Returns:
true if the instant of time represented by date object is earlier than the instant represented by dateToCompare; false otherwise.

isBeforeEquals

public static boolean isBeforeEquals(Date date,
                                     Date dateToCompare)
Tests if the date is before or equals than the specified dateToCompare.

Parameters:
date - the date to compare with the dateToCompare.
dateToCompare - the date to compare with the date.
Returns:
true if the instant of time represented by date object is earlier or equal than the instant represented by dateToCompare; false otherwise.

isAfterEquals

public static boolean isAfterEquals(Date date,
                                    Date dateToCompare)
Tests if the date is after or equals than the specified dateToCompare.

Parameters:
date - the date to compare with the dateToCompare.
dateToCompare - the date to compare with the date.
Returns:
true if the instant of time represented by date object is later or equal than the instant represented by dateToCompare; false otherwise.

isAfter

public static boolean isAfter(Date date,
                              Date dateToCompare)
Tests if the date is after than the specified dateToCompare.

Parameters:
date - the date to compare with the dateToCompare.
dateToCompare - the date to compare with the date.
Returns:
true if the instant of time represented by date object is later than the instant represented by dateToCompare; false otherwise.

periodsOverlap

public static boolean periodsOverlap(Date startDate1,
                                     Date endDate1,
                                     Date startDate2,
                                     Date endDate2)
Returns true if two periods overlap

Parameters:
startDate1 - the period one start date
endDate1 - the period one end date
startDate2 - the period two start date
endDate2 - the period two end date
Returns:
true if overlap

containsPeriod

public static boolean containsPeriod(Date startDate1,
                                     Date endDate1,
                                     Date startDate2,
                                     Date endDate2)
Returns true if the first period contains the second periods

Parameters:
startDate1 - the period one start date
endDate1 - the period one end date
startDate2 - the period two start date
endDate2 - the period two end date
Returns:
true if the first period contains the second period

tomorrow

public static Date tomorrow()
Returns:
a day after today

today

public static Date today()

yesterday

public static Date yesterday()
Returns:
a day before today

monthsAway

public static Date monthsAway(int months)
Parameters:
months - amount of months to move the calendar
Returns:
a date that is months in the future/past. Use negative values for past dates.

oneMonthInFuture

public static Date oneMonthInFuture()
Returns:
a date that is one month in the future

oneMonthInPast

public static Date oneMonthInPast()
Returns:
a date that is one month in the past

getLastDayOfMonth

public static Date getLastDayOfMonth(Date date)
Parameters:
date - Date that includes the desired month in order to calculate the last day of that month
Returns:
the date of the last day of the month

formatDuration

public static String formatDuration(long duration)


Copyright © 2014. All rights reserved.