utils
Class DateUtils

java.lang.Object
  extended by utils.DateUtils

public class DateUtils
extends java.lang.Object


Field Summary
static java.text.DateFormat DATE_HEADER_FORMAT
          Format used for SMTP (and probably other) Date headers.
static java.text.DateFormat EUROPEAN_DATE_FORMAT
           
static java.lang.String EUROPEAN_DATE_PATTERN
           
static java.text.DateFormat EUROPEAN_DATE_TIME_FORMAT
           
static java.lang.String EUROPEAN_DATE_TIME_PATTERN
           
static java.text.SimpleDateFormat ISO8601_DATE_FORMAT
           
static java.lang.String ISO8601_DATE_PATTERN
          ISO8601-like pattern for date.
static java.lang.String ISO8601_DATETIME_PATTERN
           
static java.lang.String ISO8601_TIME_PATTERN
          ISO8601-like pattern for time.
static java.text.SimpleDateFormat SIMPLE_DATE_FORMAT
           
static java.lang.String SIMPLE_DATE_PATTERN
           
static java.text.DateFormat TIME_HOUR_MIN_FORMAT
           
static java.text.DateFormat TIME_HOUR_MIN_SECOND_FORMAT
           
 
Constructor Summary
DateUtils()
           
 
Method Summary
static java.util.Date addDays(java.util.Date startDate, int numberOfDays)
           
static java.lang.String format(java.util.Date date, java.lang.String pattern)
          Format a date/time into a specific pattern.
static java.lang.String format(long date, java.lang.String pattern)
          Format a date/time into a specific pattern.
static java.lang.String formatElapsedTime(long millis)
          Format an elapsed time into a plurialization correct string.
static java.util.GregorianCalendar getCalendar()
           
static java.util.GregorianCalendar getCalendar(java.util.Date d)
           
static java.util.GregorianCalendar getCalendar(java.lang.String s)
           
static java.util.GregorianCalendar getCalendarForNow()
           
static java.util.Date getCommaLessFullNameDate(java.lang.String dateString)
          Inputs a FQDN (Fully qualified date name) and returns an instance of a java.util.Date
static java.util.Date getDate()
          prompt the user for a date
static java.util.Date getDate(java.util.Date d)
           
static java.util.Date getDate(java.lang.String dateString)
           
static java.util.Date getDateFromEEEMMMddyyyy(java.lang.String dateString)
           
static java.util.Date getDateFromMMMddyyyy(java.lang.String dateString)
           
static java.lang.String[] getDateStrings()
           
static int getDay(java.util.Date date)
           
static java.util.Date getEuropeanDate(java.lang.String dateString)
           
static java.lang.String getEuropeanDateString(java.util.Date d)
           
static java.lang.String getEuropeanDateTimeString(java.util.Date d)
           
static int getHour(java.util.Date date)
           
static java.lang.String getISO8601_String(java.util.GregorianCalendar gc)
           
static java.util.Date getLongDate(java.lang.String dateString)
          Inputs a string, filters out the control characters and trims off the spaces.
static java.util.Date getLongDate(java.lang.String[] s)
          Try a bunch of strings until you find a valid date.
static int getMinute(java.util.Date date)
           
static java.lang.String[] getMonthStrings()
          DateFormatSymbols returns an extra, empty value affineTransform the end of the array of months.
static double getNumberDays(long l)
           
static int getNumberOfDays(java.util.Date d1, java.util.Date d2)
           
static int getPhaseOfMoon(java.util.Calendar cal)
          Calculate the phase of the moon for a given date.
static int getSecond(java.util.Date date)
           
static java.util.Date getShortDate(java.lang.String dateString)
          Use short date format: "10/1/07"
static java.util.Date getSimpleDate(java.lang.String s)
          Input a simple date in M/d/yy format.
static java.lang.String getSimpleDateString(java.util.Date d)
          Format the date using the DateUtils.SIMPLE_DATE_FORMAT
static java.lang.String getSMTPDateString()
          Returns the current Date in a format suitable for a SMTP date header.
static java.lang.String getSMTPDateString(java.util.Calendar cal)
           
static java.lang.String getSqlDate(java.util.Date d)
           
static java.lang.String getTimeStringHH_MM_PM_AM(java.util.Date d)
           
static java.lang.String getTimeStringHMS(java.util.Date d)
           
static java.util.Calendar getTommrowAt(int hour, int minutes, int seconds)
           
static java.util.Date[][] getWindows(java.util.Date startDate, int windowIncrement, int numberOfWindows)
           
static java.util.Date getYahooDate(java.lang.String dateString)
          20-Jul-07
static int getYear(java.util.Date date)
           
static boolean isWeekend(java.util.GregorianCalendar gc)
           
static void main(java.lang.String[] args)
           
static java.util.Date parseIso8601Date(java.lang.String datestr)
          Parse a string as a date using the ISO8601_DATE format which is yyyy-MM-dd
static java.util.Date parseIso8601DateTime(java.lang.String datestr)
          Parse a string as a datetime using the ISO8601_DATETIME format which is yyyy-MM-dd'T'HH:mm:ss
static java.util.Date parseIso8601DateTimeOrDate(java.lang.String datestr)
          Parse a string as a date using the either the ISO8601_DATETIME or ISO8601_DATE formats.
static long subtract(java.util.Calendar end, java.util.Calendar start)
          Compute end - start times
static long subtract(java.util.Date d1, java.util.Date d2)
           
static java.util.Date testAddDays()
           
static void testDateFormat()
           
static void testFullNameDate()
          Inputs a FQDN (Fully qualified date name) and returns an instance of a java.util.Date
static void testGetEuropeanDate()
           
static void testGetEuropeanDateTimeString()
           
static void testGetNumberOfDate()
           
static void testGetTommrowAt()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIMPLE_DATE_PATTERN

public static final java.lang.String SIMPLE_DATE_PATTERN
See Also:
Constant Field Values

SIMPLE_DATE_FORMAT

public static final java.text.SimpleDateFormat SIMPLE_DATE_FORMAT

TIME_HOUR_MIN_SECOND_FORMAT

public static final java.text.DateFormat TIME_HOUR_MIN_SECOND_FORMAT

TIME_HOUR_MIN_FORMAT

public static final java.text.DateFormat TIME_HOUR_MIN_FORMAT

EUROPEAN_DATE_PATTERN

public static final java.lang.String EUROPEAN_DATE_PATTERN
See Also:
Constant Field Values

EUROPEAN_DATE_FORMAT

public static final java.text.DateFormat EUROPEAN_DATE_FORMAT

EUROPEAN_DATE_TIME_PATTERN

public static final java.lang.String EUROPEAN_DATE_TIME_PATTERN
See Also:
Constant Field Values

EUROPEAN_DATE_TIME_FORMAT

public static final java.text.DateFormat EUROPEAN_DATE_TIME_FORMAT

ISO8601_DATETIME_PATTERN

public static final java.lang.String ISO8601_DATETIME_PATTERN
See Also:
Constant Field Values

ISO8601_DATE_PATTERN

public static final java.lang.String ISO8601_DATE_PATTERN
ISO8601-like pattern for date. yyyy-MM-dd

See Also:
Constant Field Values

ISO8601_DATE_FORMAT

public static final java.text.SimpleDateFormat ISO8601_DATE_FORMAT

ISO8601_TIME_PATTERN

public static final java.lang.String ISO8601_TIME_PATTERN
ISO8601-like pattern for time. HH:mm:ss

See Also:
Constant Field Values

DATE_HEADER_FORMAT

public static final java.text.DateFormat DATE_HEADER_FORMAT
Format used for SMTP (and probably other) Date headers.

Constructor Detail

DateUtils

public DateUtils()
Method Detail

getDay

public static int getDay(java.util.Date date)

getHour

public static int getHour(java.util.Date date)

getSecond

public static int getSecond(java.util.Date date)

getMinute

public static int getMinute(java.util.Date date)

getYear

public static int getYear(java.util.Date date)

getDate

public static java.util.Date getDate()
prompt the user for a date

Returns:
a date instance

getSimpleDate

public static java.util.Date getSimpleDate(java.lang.String s)
Input a simple date in M/d/yy format. Performs error correction with a modal dialog. returns todays date if there is an error.

Parameters:
s -
Returns:

getLongDate

public static java.util.Date getLongDate(java.lang.String[] s)
Try a bunch of strings until you find a valid date.

Parameters:
s -
Returns:
null if you can't find a valid date.

getLongDate

public static java.util.Date getLongDate(java.lang.String dateString)
Inputs a string, filters out the control characters and trims off the spaces. Uses formats like: "april 27, 2006"

Parameters:
dateString - an input string that may contain control characters.
Returns:
java.util.Date

getDate

public static java.util.Date getDate(java.util.Date d)

getSimpleDateString

public static java.lang.String getSimpleDateString(java.util.Date d)
Format the date using the DateUtils.SIMPLE_DATE_FORMAT

Parameters:
d - input date to be formatted.
Returns:
string in mm/dd/yyyy format

getEuropeanDateString

public static java.lang.String getEuropeanDateString(java.util.Date d)

getEuropeanDateTimeString

public static java.lang.String getEuropeanDateTimeString(java.util.Date d)

testGetEuropeanDateTimeString

public static void testGetEuropeanDateTimeString()

testGetEuropeanDate

public static void testGetEuropeanDate()
                                throws java.text.ParseException
Throws:
java.text.ParseException

testFullNameDate

public static void testFullNameDate()
                             throws java.text.ParseException
Inputs a FQDN (Fully qualified date name) and returns an instance of a java.util.Date

Throws:
java.text.ParseException

getCommaLessFullNameDate

public static java.util.Date getCommaLessFullNameDate(java.lang.String dateString)
                                               throws java.text.ParseException
Inputs a FQDN (Fully qualified date name) and returns an instance of a java.util.Date

Throws:
java.text.ParseException

getSqlDate

public static java.lang.String getSqlDate(java.util.Date d)

getEuropeanDate

public static java.util.Date getEuropeanDate(java.lang.String dateString)
                                      throws java.text.ParseException
Throws:
java.text.ParseException

getYahooDate

public static java.util.Date getYahooDate(java.lang.String dateString)
20-Jul-07

Parameters:
dateString -
Returns:
Throws:
java.text.ParseException

getDate

public static java.util.Date getDate(java.lang.String dateString)
                              throws java.text.ParseException
Throws:
java.text.ParseException

getShortDate

public static java.util.Date getShortDate(java.lang.String dateString)
                                   throws java.text.ParseException
Use short date format: "10/1/07"

Parameters:
dateString - is in short date format
Returns:
java.util.Date instance
Throws:
java.text.ParseException

testGetNumberOfDate

public static void testGetNumberOfDate()
                                throws java.text.ParseException
Throws:
java.text.ParseException

getNumberDays

public static double getNumberDays(long l)

subtract

public static long subtract(java.util.Date d1,
                            java.util.Date d2)

getTimeStringHMS

public static java.lang.String getTimeStringHMS(java.util.Date d)

getTimeStringHH_MM_PM_AM

public static java.lang.String getTimeStringHH_MM_PM_AM(java.util.Date d)

format

public static java.lang.String format(long date,
                                      java.lang.String pattern)
Format a date/time into a specific pattern.

Parameters:
date - the date to format expressed in milliseconds.
pattern - the pattern to use to format the date.
Returns:
the formatted date.

format

public static java.lang.String format(java.util.Date date,
                                      java.lang.String pattern)
Format a date/time into a specific pattern.

Parameters:
date - the date to format expressed in milliseconds.
pattern - the pattern to use to format the date.
Returns:
the formatted date.

formatElapsedTime

public static java.lang.String formatElapsedTime(long millis)
Format an elapsed time into a plurialization correct string. It is limited only to report elapsed time in minutes and seconds and has the following behavior.

Parameters:
millis - the elapsed time to report in milliseconds.
Returns:
the formatted text in minutes/seconds.

getPhaseOfMoon

public static int getPhaseOfMoon(java.util.Calendar cal)
Calculate the phase of the moon for a given date.

Code heavily influenced by hacklib.c in Nethack

The Algorithm:

 moon period = 29.53058 days ~= 30, year = 365.2422 days
 

days moon phase advances on first day of year compared to preceding year = 365.2422 - 12*29.53058 ~= 11

years in Metonic cycle (time until same phases fall on the same days of the month) = 18.6 ~= 19

moon phase on first day of year (epact) ~= (11*(year%19) + 18) % 30 (18 as initial condition for 1900)

current phase in days = first day phase + days elapsed in year

6 moons ~= 177 days 177 ~= 8 reported phases * 22 + 11/22 for rounding

Parameters:
cal - the calander.
Returns:
The phase of the moon as a number between 0 and 7 with 0 meaning new moon and 4 meaning full moon.
Since:
1.2, Ant 1.5

getSMTPDateString

public static java.lang.String getSMTPDateString()
Returns the current Date in a format suitable for a SMTP date header.

Returns:
the current date.
Since:
Ant 1.5.2

getSMTPDateString

public static java.lang.String getSMTPDateString(java.util.Calendar cal)

parseIso8601DateTime

public static java.util.Date parseIso8601DateTime(java.lang.String datestr)
                                           throws java.text.ParseException
Parse a string as a datetime using the ISO8601_DATETIME format which is yyyy-MM-dd'T'HH:mm:ss

Parameters:
datestr - string to be parsed
Returns:
a java.util.Date object as parsed by the format.
Throws:
java.text.ParseException - if the supplied string cannot be parsed by this pattern.
Since:
Ant 1.6

parseIso8601Date

public static java.util.Date parseIso8601Date(java.lang.String datestr)
                                       throws java.text.ParseException
Parse a string as a date using the ISO8601_DATE format which is yyyy-MM-dd

Parameters:
datestr - string to be parsed
Returns:
a java.util.Date object as parsed by the format.
Throws:
java.text.ParseException - if the supplied string cannot be parsed by this pattern.
Since:
Ant 1.6

parseIso8601DateTimeOrDate

public static java.util.Date parseIso8601DateTimeOrDate(java.lang.String datestr)
                                                 throws java.text.ParseException
Parse a string as a date using the either the ISO8601_DATETIME or ISO8601_DATE formats.

Parameters:
datestr - string to be parsed
Returns:
a java.util.Date object as parsed by the formats.
Throws:
java.text.ParseException - if the supplied string cannot be parsed by either of these patterns.
Since:
Ant 1.6

isWeekend

public static boolean isWeekend(java.util.GregorianCalendar gc)

getCalendarForNow

public static java.util.GregorianCalendar getCalendarForNow()

getCalendar

public static java.util.GregorianCalendar getCalendar(java.util.Date d)

getCalendar

public static java.util.GregorianCalendar getCalendar(java.lang.String s)
                                               throws java.text.ParseException
Throws:
java.text.ParseException

getCalendar

public static java.util.GregorianCalendar getCalendar()
                                               throws java.text.ParseException
Throws:
java.text.ParseException

getWindows

public static java.util.Date[][] getWindows(java.util.Date startDate,
                                            int windowIncrement,
                                            int numberOfWindows)
Parameters:
startDate - date to start window at, always first in list.
windowIncrement - can be negative, given in days
numberOfWindows - final date is windowIncrement * numberOfWindows from start date
Returns:
array of windows.

testGetTommrowAt

public static void testGetTommrowAt()

getTommrowAt

public static java.util.Calendar getTommrowAt(int hour,
                                              int minutes,
                                              int seconds)

subtract

public static long subtract(java.util.Calendar end,
                            java.util.Calendar start)
Compute end - start times

Parameters:
end - time, period given in calendar
start - time, period given in calendar
Returns:
the time, in milliseconds, between these two times.

addDays

public static java.util.Date addDays(java.util.Date startDate,
                                     int numberOfDays)
Parameters:
startDate - the start date
numberOfDays - offset from startDate, can be negative
Returns:
a new date offset from startDate by numberOfDays

testAddDays

public static java.util.Date testAddDays()

getISO8601_String

public static java.lang.String getISO8601_String(java.util.GregorianCalendar gc)

getNumberOfDays

public static int getNumberOfDays(java.util.Date d1,
                                  java.util.Date d2)
Parameters:
d1 -
d2 -
Returns:
the number of days between two dates

getDateFromEEEMMMddyyyy

public static java.util.Date getDateFromEEEMMMddyyyy(java.lang.String dateString)

getDateFromMMMddyyyy

public static java.util.Date getDateFromMMMddyyyy(java.lang.String dateString)

getDateStrings

public static java.lang.String[] getDateStrings()

getMonthStrings

public static java.lang.String[] getMonthStrings()
DateFormatSymbols returns an extra, empty value affineTransform the end of the array of months. Remove it.


main

public static void main(java.lang.String[] args)
                 throws java.text.ParseException
Throws:
java.text.ParseException

testDateFormat

public static void testDateFormat()
                           throws java.text.ParseException
Throws:
java.text.ParseException