/*::.
==================================================================================================================================
=================================================¦ Copyright © 2008 Allen Baker ¦=================================================
+------------------------------+
File: UDateTime.java
Originator: Allen Baker (2008.09.28 10:12)
LayoutRev: 5
================================================================================================================================== */
/*.
==========================================================================================
Package
------------------------------------------------------------------------------------------ */
package cosmicabyss.com.lib;
/*.
==========================================================================================
Imports
------------------------------------------------------------------------------------------ */
import java.io.*;
import java.util.*;
import java.text.*;
/*::
======================================================================================================================== *//**
This is a utilities class that contains static methods that supplement the Date class. It is an application of the
"Utility Pattern".<P>
The Utility pattern is used for classes that do not require instantiation and only have static methods. As the utility
class is stateless, all parameters in each method must pass all necessary information to the method.
<P>
<DL>
<DT>
<B>
Example usage:
</B>
<DD>
<BLOCKQUOTE>
<PRE id="unindent">
no example provided
</PRE>
</BLOCKQUOTE>
</DD>
</DT>
<DT>
<B>
View Source:
</B>
<DD>
<A href="UDateTime.java.html">
uDateTime.java
</A>
</DD>
</DT>
<DT>
<B>
Author:
</B>
<DD>
<A href="mailto:sourcecode.v01@cosmicabyss.com">
Allen Baker
</A>
</DD>
</DT>
</DL>
*//*
======================================================================================================================== */
public class UDateTime
{
/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[ Public ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */
/*.
==========================================================================================
Class Constants
<BLOCKQUOTE>
<PRE id="unindent">
FMTDT_STD : the standard date time format
FMTDT_XCL : the Excel default date time format
FMTDT_ASO : the ASO ticket extract DB date time format
FMTDT_HPSC : the HPSC extract date time format
</PRE>
</BLOCKQUOTE>
------------------------------------------------------------------------------------------ */
public static final String FMTDT_STD = "yyyy.MM.dd HH:mm:ss";
public static final String FMTDT_XCL = "yyyy/MM/dd HH:mm:ss" ;
public static final String FMTDT_ASO = "M/dd/yyyy HH:mm:ss" ;
public static final String FMTDT_HPSC = "yyyy/MM/dd HH:mm:ss" ;
public static final String FMTDT_CAROL = "M/dd/yyyy HH:mm" ;
/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[ Protected ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */
/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[ Private ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */
/*.
==========================================================================================
Class Constants
CALENDAR : the calendar singleton for the default time zone and locale.
------------------------------------------------------------------------------------------ */
private static Calendar CALENDAR = Calendar.getInstance();
/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[ Inner Classes ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */
/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[ Public Static Methods ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */
/**
/*: :METHOD:000:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the last millisecond of the specified date.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#000">View source</A>
@return
Last millisecond of pDate
@param
Date Date to calculate end of day from
*//*
---------------------------------------------------------------------------------------------------- */
public static Date endOfDay(Date pDate)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTime(pDate);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
return calendar.getTime();
}
}
/*: :METHOD:001:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the fiscal year in which a given date falls. It uses the November - October
fiscal year.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#001">View source</A>
@return
This method returns the fiscal year within which a specified date falls.
@param
pDate Date to calculate fiscal year from
*//*
---------------------------------------------------------------------------------------------------- */
public static int fiscalYear(Date pDate)
{
int calYr = UDateTime.year(pDate);
int calMo = UDateTime.month(pDate);
int fscYr = (calMo < 10)? calYr : calYr + 1;
return fscYr;
}
/*: :METHOD:002:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the fiscal quarter in which a given date falls. It uses the November - October
fiscal year.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#002">View source</A>
@return
This method returns the fiscal quarter within which a specified date falls.
@param
pDate Date to calculate fiscal quarter from
*//*
---------------------------------------------------------------------------------------------------- */
public static int fiscalQuarter(Date pDate)
{
int calMo = UDateTime.month(pDate);
switch (calMo)
{
default : // reqrd so dont have to put return stmt at end of method
case 10 : // Nov
case 11 : // Dec
case 0 : // Jan
return 1;
case 1 : // Feb
case 2 : // Mar
case 3 : // Apr
return 2;
case 4 : // May
case 5 : // Jun
case 6 : // Jul
return 3;
case 7 : // Aug
case 8 : // Sep
case 9 : // Oct
return 4;
}
}
/*: :METHOD:003:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the year component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#003">View source</A>
@return
This method returns the year component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int year(Date pDate)
{
return UDateTime.getField(pDate,Calendar.YEAR);
}
/*: :METHOD:004:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the month component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#004">View source</A>
@return
This method returns the month component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int month(Date pDate)
{
return UDateTime.getField(pDate,Calendar.MONTH);
}
/*: :METHOD:005:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the day component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#005">View source</A>
@return
This method returns the day component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int day(Date pDate)
{
return UDateTime.getField(pDate,Calendar.DAY_OF_MONTH);
}
/*: :METHOD:006:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the 12 hour component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#006">View source</A>
@return
This method returns the 12 hour component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int hour12(Date pDate)
{
return UDateTime.getField(pDate,Calendar.HOUR);
}
/*: :METHOD:007:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the 24 component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#007">View source</A>
@return
This method returns the 24 component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int hour24(Date pDate)
{
return UDateTime.getField(pDate,Calendar.HOUR_OF_DAY);
}
/*: :METHOD:008:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the minute component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#008">View source</A>
@return
This method returns the minute component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int minute(Date pDate)
{
return UDateTime.getField(pDate,Calendar.MINUTE);
}
/*: :METHOD:009:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the second component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#009">View source</A>
@return
This method returns the second component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int second(Date pDate)
{
return UDateTime.getField(pDate,Calendar.SECOND);
}
/*: :METHOD:010:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the millisecond component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#010">View source</A>
@return
This method returns the millisecond component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
*//*
---------------------------------------------------------------------------------------------------- */
public static int millisecond(Date pDate)
{
return UDateTime.getField(pDate,Calendar.MILLISECOND);
}
/*: :METHOD:011:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method extracts the specified component from a Date
<P><B>Implementation: </B><A HREF="UDateTime.java.html#011">View source</A>
@return
This method returns the specified component of the specified Date
@param
pDate Date to from which the component is extracted and returned.
@param
pField identifies the component to extract and return. It's value can be any of the following:
<BLOCKQUOTE>
<PRE id="unindent">
Calendar.YEAR
Calendar.MONTH_OF_YEAR
Calendar.DAY_OF_MONTH
Calendar.HOUR (for the hour in 12 hour format)
Calendar.HOUR_OF_DAY (for the hour in 24 hour format)
Calendar.MINUTE
Calendar.SECOND
Calendar.MILLISECOND
or any of the other integer fields defined for the JDK SE Calendar class.
</PRE>
</BLOCKQUOTE>
*//*
---------------------------------------------------------------------------------------------------- */
public static int getField(Date pDate, int pField)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTime(pDate);
return calendar.get(pField);
}
}
/*: :METHOD:012:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns a new Date with the hours, milliseconds, seconds and minutes set to 0.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#012">View source</A>
@return
The mDate representation of the starting instant of the specified date.
@param
pDate Date used in calculating start of day
*//*
---------------------------------------------------------------------------------------------------- */
public static Date startOfDay(Date pDate)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTime(pDate);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
return calendar.getTime();
}
}
/*: :METHOD:013:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the millisecond that a specified date began on.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#013">View source</A>
@return
This method returns the millisecond that a specified date began on.
@param
pDate long used in calculating start of day
*//*
---------------------------------------------------------------------------------------------------- */
public static long startOfDayInMillis(long pDate)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
return calendar.getTimeInMillis();
}
}
/*: :METHOD:014:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the millisecond that a specified date ended on.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#014">View source</A>
@return
This method returns the millisecond that a specified date ended on.
@param
pDate long used in calculating start of day
*//*
---------------------------------------------------------------------------------------------------- */
public static long endOfDayInMillis(long pDate)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MILLISECOND, 999);
calendar.set(Calendar.SECOND, 59);
calendar.set(Calendar.MINUTE, 59);
return calendar.getTimeInMillis();
}
}
/*: :METHOD:015:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns a Date representing the date and time exactly 1 day after the specified date.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#015">View source</A>
@return
This method returns a Date representing the date and time exactly 1 day after the specified date.
@param
pDate Date used in calculating one day forward.
*//*
---------------------------------------------------------------------------------------------------- */
public static Date nextDay(Date pDate)
{
return new Date(UDateTime.addDays(pDate.getTime(), 1));
}
/*: :METHOD:016:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method adds a specified number of days to a time and returns the resulting time in
milliseconds.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#016">View source</A>
@return
This method adds a specified number of days to a time and returns the resulting time in
milliseconds.
@param
pTime is the date/time in milliseconds to add to.
@param
pDays number of days in milliseconds to add to pTime to get the result that this method returns.
*//*
---------------------------------------------------------------------------------------------------- */
public static long addDays(long pTime, int pDays)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pTime);
calendar.add(Calendar.DAY_OF_MONTH, pDays);
return calendar.getTimeInMillis();
}
}
/*: :METHOD:017:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns a long representing the date/time in milliseconds at exactly 1 day after the
specified date/time.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#017">View source</A>
@return
This method returns a long representing the date/time in milliseconds at exactly 1 day after the
specified date/time.
@param
pDate is the date/time in mlliseconds used in calculating one day forward.
*//*
---------------------------------------------------------------------------------------------------- */
public static long nextDay(long pDate)
{
return UDateTime.addDays(pDate, 1);
}
/*: :METHOD:018:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns a long representing the date/time in milliseconds at exactly 1 week (7 days)
after the specified date/time.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#018">View source</A>
@return
This method returns a long representing the date/time in milliseconds at exactly 1 week after the
specified date/time.
@param
pDate is the date/time in mlliseconds used in calculating one week forward.
*//*
---------------------------------------------------------------------------------------------------- */
public static long nextWeek(long pDate)
{
return UDateTime.addDays(pDate, 7);
}
/*: :METHOD:019:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the number of days difference between two date/times that are specified in
milliseconds. The result is always positive meaning the earlier date/time is always subtracted form
the later date/time yielding the delta in days between the two date/times.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#019">View source</A>
@return
This method returns the number of days difference between two date/times that are specified in
milliseconds.
@param
pDateTime1 is the first time.
@param
pDateTime1 is the second time.
@param
CheckOverflow indicates whether to check for overflow
*//*
---------------------------------------------------------------------------------------------------- */
public static int getDaysDiff(long pDateTime1, long pDateTime2, boolean checkOverflow)
{
if (pDateTime1 > pDateTime2)
{
long tmp = pDateTime1;
pDateTime1 = pDateTime2;
pDateTime2 = tmp;
}
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDateTime1);
int delta = 0;
while (calendar.getTimeInMillis() < pDateTime2)
{
calendar.add(Calendar.DAY_OF_MONTH, 1);
delta++;
}
if (checkOverflow && (calendar.getTimeInMillis() > pDateTime2))
{
delta--;
}
return delta;
}
}
/*: :METHOD:020:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the number of days difference between two date/times that are specified in
milliseconds, implicitly checking for overflow. The result is always positive meaning the earlier
date/time is always subtracted form the later date/time yielding the delta in days between the two
date/times.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#020">View source</A>
@return
This method returns the number of days difference between two date/times that are specified in
milliseconds.
@param
pDateTime1 is the first time.
@param
pDateTime1 is the second time.
*//*
---------------------------------------------------------------------------------------------------- */
public static int getDaysDiff(long pDateTime1, long pDateTime2)
{
return UDateTime.getDaysDiff(pDateTime1, pDateTime2, true);
}
/*: :METHOD:021:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method check whether the date passed in is the first day of the year.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#021">View source</A>
@return
True if pDate corresponds to the first day of a year
@param
pDate is the date/time to check in milliseconds
@see
Date#getTime()
*//*
---------------------------------------------------------------------------------------------------- */
public static boolean isFirstOfYear(long pDate)
{
boolean ret = false;
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
int currentYear = calendar.get(Calendar.YEAR);
// Check yesterday
calendar.add(Calendar.DATE,-1);
int yesterdayYear = calendar.get(Calendar.YEAR);
ret = (currentYear != yesterdayYear);
}
return ret;
}
/*: :METHOD:022:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method checks whether the date passed in is the first day of the current month.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#022">View source</A>
@return
True if pDate corresponds to the first day of the current month
@param
pDate is the date/time to check in milliseconds
@see
Date#getTime()
*//*
---------------------------------------------------------------------------------------------------- */
public static boolean isFirstOfMonth(long pDate)
{
boolean ret = false;
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
int currentMonth = calendar.get(Calendar.MONTH);
// Check yesterday
calendar.add(Calendar.DATE,-1);
int yesterdayMonth = calendar.get(Calendar.MONTH);
ret = (currentMonth != yesterdayMonth);
}
return ret;
}
/*: :METHOD:023:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the date/time in muilliseconds at exactly 1 day before a specified date/time.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#023">View source</A>
@return
This method returns the date/time in muilliseconds at exactly 1 day before a specified date/time.
@param
pDate is the date/time used in calculating previous day
*//*
---------------------------------------------------------------------------------------------------- */
public static long previousDay(long pDate)
{
return UDateTime.addDays(pDate, -1);
}
/*: :METHOD:024:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the date/time in muilliseconds at exactly 1 week before a specified date/time.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#024">View source</A>
@return
This method returns the date/time in muilliseconds at exactly 1 week before a specified
date/time.
@param
pDate is the date/time used in calculating previous week
*//*
---------------------------------------------------------------------------------------------------- */
public static long previousWeek(long pDate)
{
return UDateTime.addDays(pDate, -7);
}
/*: :METHOD:025:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the first day before a specified Date that has the day of week matching
pStartOfWeek. For example, if you want to find the previous Monday relative to date, you would call:
GetPreviousDay(date,Calendar.MONDAY).
<P><B>Implementation: </B><A HREF="UDateTime.java.html#025">View source</A>
@return
This method returns the millisecond representation of the start of week, return value will have 0
hours, 0 minutes, 0 seconds and 0 ms.
@param
pDate is the base date form which to calculate the result
@param
PtartOfWeek Calendar constant correspoding to start of week.
*//*
---------------------------------------------------------------------------------------------------- */
public static long getPreviousDay(long pDate, int pStartOfWeek)
{
return UDateTime.getDay(pDate, pStartOfWeek, -1);
}
/*: :METHOD:026:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the first day after a specified Date that has the day of week matching
pStartOfWeek. For example, if you want to find the next monday relative to date you would call:
GetNextDay(date,Calendar.MONDAY).
<P><B>Implementation: </B><A HREF="UDateTime.java.html#026">View source</A>
@return
This method returns the millisecond representation of the start of week, return value will have 0
hours, 0 minutes, 0 seconds and 0 ms.
@param
pDate is the base date form which to calculate the result
@param
pStartOfWeek Calendar constant correspoding to start of week.
*//*
---------------------------------------------------------------------------------------------------- */
public static long getNextDay(long pDate, int pStartOfWeek)
{
return UDateTime.getDay(pDate, pStartOfWeek, 1);
}
/*: :METHOD:027:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the first day after a specified Date that has the day of week matching
pStartOfWeek. For example, if you want to find the next monday relative to date you would call:
GetNextDay(date,Calendar.MONDAY).
<P><B>Implementation: </B><A HREF="UDateTime.java.html#027">View source</A>
@return
This method returns the Date representation of the start of week, return value will have 0 hours,
0 minutes, 0 seconds and 0 ms.
@param
pDate is the base date form which to calculate the result
@param
pStartOfWeek Calendar constant correspoding to start of week.
*//*
---------------------------------------------------------------------------------------------------- */
public static Date getNextDay(Date pDate, int pStartOfWeek)
{
long lDate = pDate.getTime();
return new Date(UDateTime.getDay(lDate, pStartOfWeek, 1));
}
/*: :METHOD:028:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the day a specified number of days after a specified Date that has the day of
week matching pStartOfWeek. For example, if you want to find the second monday relative to date you
would call:
GetNextDay(date,Calendar.MONDAY, 2).
<P><B>Implementation: </B><A HREF="UDateTime.java.html#028">View source</A>
@return
This method returns the Date representation of the start of week, return value will have 0 hours,
0 minutes, 0 seconds and 0 ms.
@param
pDate is the base date form which to calculate the result
@param
pStartOfWeek Calendar constant correspoding to start of week.
@param
pIncrement is the number pStartOfWeeks to look ahead.
*//*
---------------------------------------------------------------------------------------------------- */
public static long getDay(long pDate, int pStartOfWeek, int pIncrement)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
int day = calendar.get(Calendar.DAY_OF_WEEK);
// Normalize the view starting date to a week starting day
while (day != pStartOfWeek)
{
calendar.add(Calendar.DATE, pIncrement);
day = calendar.get(Calendar.DAY_OF_WEEK);
}
return UDateTime.startOfDayInMillis(calendar.getTimeInMillis());
}
}
/*: :METHOD:029:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the previous month.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#029">View source</A>
@return
Previous month
@param
pDate is the base date form which to calculate the result
*//*
---------------------------------------------------------------------------------------------------- */
public static long getPreviousMonth(long pDate)
{
return UDateTime.incrementMonth(pDate, -1);
}
/*: :METHOD:030:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the next month.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#030">View source</A>
@return
Next month
@param
pDate is the base date form which to calculate the result
*//*
---------------------------------------------------------------------------------------------------- */
public static long getNextMonth(long pDate)
{
return UDateTime.incrementMonth(pDate, 1);
}
/*: :METHOD:031:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the date/time in milliseconds at exactly pIncrement months from pDate.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#031">View source</A>
@return
This method returns the date/time in milliseconds at exactly pIncrement months from pDate.
@param
pDate is the base date form which to calculate the result
@param
pIncrement is the number of months ahead to add to pDate.
*//*
---------------------------------------------------------------------------------------------------- */
private static long incrementMonth(long pDate, int pIncrement)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
calendar.add(Calendar.MONTH, pIncrement);
return calendar.getTimeInMillis();
}
}
/*: :METHOD:032:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the date corresponding to the start of the month.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#032">View source</A>
@return
Start of month.
@param
pDate is the base date form which to calculate the result
*//*
---------------------------------------------------------------------------------------------------- */
public static long getStartOfMonth(long pDate)
{
return UDateTime.getMonth(pDate, -1);
}
/*: :METHOD:033:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the date corresponding to the end of the month.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#033">View source</A>
@return
End of month.
@param
pDate is the base date form which to calculate the result
*//*
---------------------------------------------------------------------------------------------------- */
public static long getEndOfMonth(long pDate)
{
return UDateTime.getMonth(pDate, 1);
}
/*: :METHOD:034:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns either the date/time of the first millisecond of either the first or last day of
the month in which pDate falls.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#034">View source</A>
@return
This method returns either the date/time of the first millisecond of either the first or last day
of the month in which pDate falls.
@param
pDate is the base date form which to calculate the result
@param
pStartOrEnd tells this method whether to return the start of end of the month. If it is -1, this
method returns the start. Otherwise, it returns the end.
*//*
---------------------------------------------------------------------------------------------------- */
public static long getMonth(long pDate, int pStartOrEnd)
{
long result;
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
if (pStartOrEnd == -1)
{
calendar.set(Calendar.DAY_OF_MONTH, 1);
result = startOfDayInMillis(calendar.getTimeInMillis());
}
else
{
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.add(Calendar.MILLISECOND, -1);
result = calendar.getTimeInMillis();
}
}
return result;
}
/*: :METHOD:035:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns the day of the week.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#035">View source</A>
@return
Day of week.
@param
pDate is the base date form which to calculate the result
*//*
---------------------------------------------------------------------------------------------------- */
public static int getDayOfWeek(long pDate)
{
Calendar calendar = CALENDAR;
synchronized(calendar)
{
calendar.setTimeInMillis(pDate);
return (calendar.get(Calendar.DAY_OF_WEEK));
}
}
/*: :METHOD:036:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
Parse a date/time string according to the given parse position. For example, a time text "07/10/96
4:5 PM, PDT" will be parsed into a Date that is equivalent to Date(837039928046).
<P><B>Implementation: </B><A HREF="UDateTime.java.html#036">View source</A>
@return
A Date, or null if the input could not be parsed
@param
pFormat is the format of the date/time string to be parsed
@param
pStr is the date/time string to be parsed
@see
SimpleDateFormat
*//*
---------------------------------------------------------------------------------------------------- */
public static <Type1,Type2> Date getDateFromString(Type1 pFormat, Type2 pStr)
{
String fmt = UString.toString(pFormat);
String str = UString.toString(pStr);
try
{
return (new SimpleDateFormat(fmt)).parse(str);
}
catch (Exception e)
{
return null;
}
}
/*: :METHOD:037:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns a String representation of the date argument. The String is formatted according
to the format argument.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#037">View source</A>
@return
A formatted string, or the empty string if the input could not be formatted
@param
pFormat is the format of the date/time string to be parsed
@param
pDate is the date/time to be formatted
@see
SimpleDateFormat
*//*
---------------------------------------------------------------------------------------------------- */
public static <Type> String getStringFromDate(Type pFormat, Date pDate)
{
String fmt = UString.toString(pFormat);
try
{
return (new SimpleDateFormat(fmt)).format(pDate);
}
catch (Exception e)
{
return null;
}
}
/*: :METHOD:038:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
Parse a date/time string according to the given parse position. For example, a time text "07/10/96
4:5 PM, PDT" will be parsed into a Date that is equivalent to Date(837039928046).
<P><B>Implementation: </B><A HREF="UDateTime.java.html#038">View source</A>
@return
A long, or 0 if the input could not be parsed
@param
pFormat is the format of the date/time string to be parsed
@param
pStr is the date/time string to be parsed
@see
SimpleDateFormat
*//*
---------------------------------------------------------------------------------------------------- */
public static <Type1,Type2> long getTimeFromString(Type1 pFormat, Type2 pStr)
{
String fmt = UString.toString(pFormat);
String str = UString.toString(pStr);
Date date = UDateTime.getDateFromString(fmt,str);
if (date == null)
{
return 0;
}
else
{
return date.getTime();
}
}
/*: :METHOD:039:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method returns a String representation of the date argument. The String is formatted according
to the format argument.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#039">View source</A>
@return
A formatted string, or the empty string if the input could not be formatted
@param
pFormat is the format of the date/time string to be parsed
@param
pTime is the date/time to be formatted
@see
SimpleDateFormat
*//*
---------------------------------------------------------------------------------------------------- */
public static <Type> String getStringFromTime(Type pFormat, long pTime)
{
String fmt = UString.toString(pFormat);
try
{
return (new SimpleDateFormat(fmt)).format(new Date(pTime));
}
catch (Exception e)
{
return null;
}
}
/*: :METHOD:040:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method converts astring representation of a date/time from one format to another.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#040">View source</A>
@return
A String containing a represntation of the date/time in the new format
@param
pDate is the date in the input format
@param
pFromFmt is a format string defining the format of pDate
@param
pToFmt is a format string defining the format of the output
*//*
---------------------------------------------------------------------------------------------------- */
public static <Type1,Type2,Type3> String convertDateStringFormat(String pDate, String pFromFmt, String pToFmt)
{
String lDate = UString.toString(pDate);
String lFromFmt = UString.toString(pFromFmt);
String lToFmt = UString.toString(pToFmt);
/*.
==========================================================================================
If the date is empty just return it instead of trying to parse it.
------------------------------------------------------------------------------------------ */
if (lDate.equals("")) return pDate;
/*.
==========================================================================================
------------------------------------------------------------------------------------------ */
Date date = UDateTime.getDateFromString(lFromFmt, lDate);
String result = UDateTime.getStringFromDate(lToFmt , date);
return result;
}
/*: :METHOD:041:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This boilerplate method is used for testing this class and may include any code the developer
chooses.
<P><B>Implementation: </B><A HREF="UDateTime.java.html#041">View source</A>
*//*
---------------------------------------------------------------------------------------------------- */
public static void test() throws Exception
{
System.out.println
(
"\"HELLO WORLD!\" I'm the UDateTime class, and I approved this message."
);
String str = "2001.09.11 06:46:26 MDT";
Date dat = UDateTime.getDateFromString("yyyy.MM.dd HH:mm:ss zzz",str);
long tim = UDateTime.getTimeFromString("yyyy.MM.dd HH:mm:ss zzz",str);
String dst = UDateTime.getStringFromDate("yyyy.MM.dd HH:mm:ss zzz",dat);
String tst = UDateTime.getStringFromTime("yyyy.MM.dd HH:mm:ss zzz",tim);
String xxx = UDateTime.convertDateStringFormat(str,"yyyy.MM.dd HH:mm:ss zzz","M/dd/yyyy h:mm:ss aa zzz");
System.out.println("original string: " + str);
System.out.println("getDateFromString: " + dat);
System.out.println("getTimeFromString: " + tim);
System.out.println("getStringFromDate: " + dst);
System.out.println("getStringFromTime: " + tst);
System.out.println("convertDateStringFormat: " + xxx);
}
/*: :METHOD:042:BOOKMARK:
====================================================================================================
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]
==================================================================================================== *//**
This method allows this class file to be unit tested as a standalone application. It's the method
that's called when the class is invoked from the command line by using the java application launcher
- "java". Main() is not a required method, but the practice of putting one in each class and
wrapping class test code within it allows easy unit testing of the class; and main does not need to
be removed when testing is complete.
<P>
<DL>
<DT>
<B>
Command line usage:
</B>
<DD>
Java cosmicabyss.com.lib.UDateTime
</DD>
</DT>
</DL>
<P><B>Implementation: </B><A HREF="UDateTime.java.html#042">View source</A>
@param
pArgs contains the command line arguments with which this class was invoked as an application.
*//*
---------------------------------------------------------------------------------------------------- */
public static void main(String[] pArgs) throws Exception
{
/*.
==========================================================================================
Test code
------------------------------------------------------------------------------------------ */
UDateTime.test();
}
} // class UDateTime
/*:.
==============================================================================================================
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@[ Notes ]@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
============================================================================================================== */