Java/Database SQL JDBC/Date Time Timestamp
Содержание
- 1 Compare two times
- 2 Construct java.sql.Timestamp from string
- 3 Convert an Object to a DateTime, without an Exception
- 4 Convert an Object to a java.sql.Time
- 5 Convert an Object to a Timestamp, without an Exception
- 6 Convert into java.sql.Time (or into java.util.Calendar)
- 7 Convert java.sql.Timestamp to long for easy compare
- 8 convert Strings to Dates and Timestamps and vice versa.
- 9 Get Date From MySql
- 10 Get date from Oracle
- 11 Get java.sql.Timestamp fro current time
- 12 Insert Date, time and date time data to Oracle
- 13 Parse date and time
- 14 Timestamp parse
Compare two times
/**
* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
*
* Project: OpenSubsystems
*
* $Id: DateUtils.java,v 1.7 2007/01/07 06:14:00 bastafidli Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Collection of useful utilities to work with dates.
*
* @version $Id: DateUtils.java,v 1.7 2007/01/07 06:14:00 bastafidli Exp $
* @author Miro Halas
* @code.reviewer Miro Halas
* @code.reviewed 1.5 2005/09/13 13:23:15 bastafidli
*/
public final class DateUtils
{
// Constants ////////////////////////////////////////////////////////////////
/**
* One second in milliseconds.
*/
public static final long ONE_SECOND = 1000L;
/**
* One minute in milliseconds.
*/
public static final long ONE_MINUTE = ONE_SECOND * 60L;
/**
* One hour in milliseconds.
*/
public static final long ONE_HOUR = ONE_MINUTE * 60L;
/**
* One day in milliseconds.
*/
public static final long ONE_DAY = ONE_HOUR * 24L;
/**
* Separator we used to separate time from the nanosecond portion of the
* timestamp when converted to string.
*/
public static final char NANO_SEPARATOR = ":";
/**
* Constant for timing type
*/
public static final int TIMING_NEVER = 0;
/**
* Constant for timing type
*/
public static final int TIMING_MINUTES = 1;
/**
* Constant for timing type
*/
public static final int TIMING_HOURS = 2;
/**
* Constant for timing type
*/
public static final int TIMING_DAYS = 3;
/**
* Constant for timing type
*/
public static final int TIMING_WEEKS = 4;
/**
* Constant for timing type
*/
public static final int TIMING_MONTHS = 5;
/**
* Constant for timing type
*/
public static final int TIMING_YEARS = 6;
/**
* Constant for timing type
*/
public static final int TIMING_NONE = 7;
/**
* Constant for current date code used in date/time formulas
*/
public static final String CURRENT_DATE_CODE = "now";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char YEAR_CODE = "y";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char MONTH_CODE = "M";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char WEEK_CODE = "w";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char DAY_CODE = "d";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char HOUR_CODE = "h";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char MINUTE_CODE = "m";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char SECOND_CODE = "s";
/**
* constant for date type DATE
*/
public static final int DATE_TYPE_DATE = 1;
/**
* constant for date type TIME
*/
public static final int DATE_TYPE_TIME = 2;
/**
* constant for date type DATETIME
*/
public static final int DATE_TYPE_DATETIME = 3;
// Constants for period start types /////////////////////////////////////////
// TODO: For Miro: Remove this code once all the code which referred to these
// constants was fixed
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_NONE = 0;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_CREATION = 1;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_COMPLETION = 2;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_APPROVAL = 3;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_ACTIVATION = 4;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_INACTIVATION = 5;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_DYNAMIC = 6;
//
// /**
// * constant for period type code
// */
// public static final int PERIOD_TYPE_CODE = 99;
//
// /**
// * constant for period type object
// */
// public static final Integer PERIOD_TYPE_OBJ = new Integer(PERIOD_TYPE_CODE);
// Cached variables /////////////////////////////////////////////////////////
/**
* static SimpleDateFormat for date format to display on UI and in messages.
*/
public static final SimpleDateFormat DATE_FORMAT
= (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT);
/**
* static SimpleDateFormat for time format to display on UI and in messages.
*/
public static final SimpleDateFormat TIME_FORMAT
= (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM);
/**
* static SimpleDateFormat for datetime format to display on UI and in messages.
*/
public static final SimpleDateFormat DATETIME_FORMAT
= (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM);
/**
* static SimpleDateFormat for date format to store date as string so that
* it is stored consistently.
*/
public static final SimpleDateFormat DATE_STORE_FORMAT
= new SimpleDateFormat("MM/dd/yyyy");
/**
* static SimpleDateFormat for time format to store time as string so that
* it is stored consistently.
*/
public static final SimpleDateFormat TIME_STORE_FORMAT
= new SimpleDateFormat("HH:mm:ss");
/**
* static SimpleDateFormat for datetime format to store date and time as
* string so that it is stored consistently.
*/
public static final SimpleDateFormat DATETIME_STORE_FORMAT
= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
/**
* static SimpleDateFormat for date format for sql date
*/
public static final SimpleDateFormat DATE_SQL_FORMAT
= new SimpleDateFormat("yyyy-MM-dd 00:00:00");
/**
* static SimpleDateFormat for time format for sql time
*/
public static final SimpleDateFormat TIME_SQL_FORMAT
= new SimpleDateFormat("1970-01-01 HH:mm:ss");
/**
* static SimpleDateFormat for datetime format for sql date and time
*/
public static final SimpleDateFormat DATETIME_SQL_FORMAT
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Constructors /////////////////////////////////////////////////////////////
/**
* Private constructor since this class cannot be instantiated
*/
private DateUtils(
)
{
// Do nothing
}
// Public methods ///////////////////////////////////////////////////////////
/**
* Check if two dates equals regardless of the time. Two null dates are equal.
* Null and not null dates are not equal.
*
* @param dtFirst - first date to compare, can be null
* @param dtSecond - second date to compare, can be null
* @return boolean - true if two dates equals regardless of what the time is
*/
public static boolean dateEquals(
Date dtFirst,
Date dtSecond
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
Calendar compCalendar;
int iEra;
int iYear;
int iMonth;
int iDay;
compCalendar = Calendar.getInstance();
compCalendar.setTime(dtFirst);
iEra = compCalendar.get(Calendar.ERA);
iYear = compCalendar.get(Calendar.YEAR);
iMonth = compCalendar.get(Calendar.MONTH);
iDay = compCalendar.get(Calendar.DATE);
compCalendar.setTime(dtSecond);
bReturn = ((iEra == compCalendar.get(Calendar.ERA))
&& (iYear == compCalendar.get(Calendar.YEAR))
&& (iMonth == compCalendar.get(Calendar.MONTH))
&& (iDay == compCalendar.get(Calendar.DATE)));
}
}
}
return bReturn;
}
/**
* Check if two times equals regardless of the date. Two null times are equal.
* Null and not null times are not equal.
*
* @param dtFirst - first time to compare, can be null
* @param dtSecond - second time to compare, can be null
* @param bIgnoreMilliseconds - if true milliseconds will be ignored in comparison
* @return boolean - true if two time equals regardless of what the date is
*/
public static boolean timeEquals(
Date dtFirst,
Date dtSecond,
boolean bIgnoreMilliseconds
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
Calendar compCalendar;
int iHour;
int iMinute;
int iSecond;
int iMili;
compCalendar = Calendar.getInstance();
compCalendar.setTime(dtFirst);
iHour = compCalendar.get(Calendar.HOUR_OF_DAY);
iMinute = compCalendar.get(Calendar.MINUTE);
iSecond = compCalendar.get(Calendar.SECOND);
iMili = compCalendar.get(Calendar.MILLISECOND);
compCalendar.setTime(dtSecond);
bReturn = ((iHour == compCalendar.get(Calendar.HOUR_OF_DAY))
&& (iMinute == compCalendar.get(Calendar.MINUTE))
&& (iSecond == compCalendar.get(Calendar.SECOND))
&& ((bIgnoreMilliseconds)
|| (iMili == compCalendar.get(Calendar.MILLISECOND))));
}
}
}
return bReturn;
}
/**
* Check if two dates and times are equal. Two null dates are equal. Null
* and not null dates are not equal.
*
* @param dtFirst - first date time to compare, can be null
* @param dtSecond - second date time to compare, can be null
* @return boolean - true if two date and times are equal
*/
public static boolean dateAndTimeEquals(
Date dtFirst,
Date dtSecond
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
// They are both not null so they have to match to millisecond
// (actually to nanosecond since the getTime takes nanoseconds
// into account)
bReturn = (dtFirst.getTime() == dtSecond.getTime());
}
}
}
return bReturn;
}
/**
* Check if String representing date is function or date. Date is a function
* (formula) if it starts with the current date/time variable which can be
* followed by expression describing period from current date.
*
* @param strValue - string representation of date or date function
* @return boolean - date function flag
*/
public static boolean isFunction(
String strValue
)
{
boolean bReturn = false;
if ((strValue != null) && (strValue.length() > 0)
&& (strValue.trim().startsWith(CURRENT_DATE_CODE)))
{
bReturn = true;
}
return bReturn;
}
/**
* Parse date time value from given string resolving any functions or formulas
* the string can contain. This method can be therefore used if the passed
* string contains string representation date, time or timestamp or a formula
* such as now + 3h - 1m + 4d.
*
* @param strValue - string representation of date or date function
* @param iDateType - date type code, one of the DATE_TYPE_XXX constants
* @param stored - flag if Date should be parsed using format used for
* storage or for display
* @return Timestamp - parsed date or null if date was null
* @throws OSSInvalidDataException - error during parsing
*/
public static Timestamp parseDateTime(
String strValue,
int iDateType,
boolean stored
)
{
Timestamp tsReturn = null;
Calendar workCal = GregorianCalendar.getInstance();
if (strValue != null && strValue.length() > 0)
{
strValue = strValue.trim();
if (strValue.startsWith(CURRENT_DATE_CODE))
{
strValue = strValue.replaceAll("[ ]", "");
// If the user specified "UseCurrent", then substitute the
// current date/time in the value
workCal.setTime(new Date());
// Log.getInstance().debug("Parsing current date " + strValue);
// Parse the date math
int iBeginIndex = CURRENT_DATE_CODE.length();
int iMaxLength = strValue.length();
int iSign = 1;
int iNumberIndex;
int iValue;
char cChar = " ";
while (iBeginIndex < iMaxLength)
{
// This has to be sign
if (strValue.charAt(iBeginIndex) == "+")
{
iSign = 1;
}
else if (strValue.charAt(iBeginIndex) == "-")
{
iSign = -1;
}
else
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iBeginIndex));
}
iBeginIndex++;
// Now we have to have number
iNumberIndex = iBeginIndex;
while (((iBeginIndex == iNumberIndex) || Character.isDigit(cChar))
&& (iBeginIndex < iMaxLength))
{
cChar = strValue.charAt(iBeginIndex++);
}
// We have to go one back because we should stop on modifier (e.g 1m)
iBeginIndex--;
try
{
iValue = Integer.parseInt(strValue.substring(iNumberIndex, iBeginIndex));
}
catch (NumberFormatException nmeExc)
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iNumberIndex));
}
// This has to be modifier: y - year, M - month, w - week,
// d - day, h - hour, m - minute, s - second
cChar = strValue.charAt(iBeginIndex);
switch(cChar)
{
case(YEAR_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used YEAR modifier for TIME type");
}
workCal.add(Calendar.YEAR, iSign * iValue);
break;
}
case(MONTH_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used MONTH modifier for TIME type");
}
workCal.add(Calendar.MONTH, iSign * iValue);
break;
}
case(WEEK_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used WEEK modifier for TIME type");
}
workCal.add(Calendar.WEEK_OF_YEAR, iSign * iValue);
break;
}
case(DAY_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used DAY modifier for TIME type");
}
workCal.add(Calendar.DATE, iSign * iValue);
break;
}
case(HOUR_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used HOUR modifier for DATE type");
}
workCal.add(Calendar.HOUR, iSign * iValue);
break;
}
case(MINUTE_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used MINUTE modifier for DATE type");
}
workCal.add(Calendar.MINUTE, iSign * iValue);
break;
}
case(SECOND_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used SECOND modifier for DATE type");
}
workCal.add(Calendar.SECOND, iSign * iValue);
break;
}
default:
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iBeginIndex));
}
}
iBeginIndex++;
}
tsReturn = new Timestamp(workCal.getTimeInMillis());
}
else
{
try
{
if (stored)
{
switch (iDateType)
{
case (DATE_TYPE_DATE) :
{
tsReturn = new Timestamp(DATE_STORE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_TIME) :
{
tsReturn = new Timestamp(TIME_STORE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_DATETIME) :
{
tsReturn = new Timestamp(DATETIME_STORE_FORMAT.parse(strValue).getTime());
break;
}
default:
{
assert false : "Unknown date type " + iDateType;
}
}
}
else
{
switch (iDateType)
{
case (DATE_TYPE_DATE) :
{
tsReturn = new Timestamp(DATE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_TIME) :
{
tsReturn = new Timestamp(TIME_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_DATETIME) :
{
tsReturn = new Timestamp(DATETIME_FORMAT.parse(strValue).getTime());
break;
}
default:
{
assert false : "Unknown date type " + iDateType;
}
}
}
}
catch (ParseException peExc)
{
throw new RuntimeException(
"Date is in incorrect format. Problems with parsing.",
peExc);
}
}
}
return tsReturn;
}
/**
* Parse the specified period into string displaying number of days the
* period represents.
*
* @param lPeriod - period in miliseconds
* @return String - period in format "x day(s)" or "" if not valid period
*/
public static String parseDayPeriod(
long lPeriod
)
{
StringBuffer sbReturn = new StringBuffer();
long lDays = 0L;
if (lPeriod > 0)
{
// we will count each started day as counted day
lPeriod = lPeriod + DateUtils.ONE_DAY - 1;
lDays = lPeriod / DateUtils.ONE_DAY;
sbReturn.append(lDays);
if (lDays == 1L)
{
sbReturn.append(" day");
}
else
{
sbReturn.append(" days");
}
}
else
{
sbReturn.append("0 days");
}
return sbReturn.toString();
}
/**
* Parse the specified period into string displaying date and time the
* period represents.
*
* @param lPeriod - preiod in miliseconds
* @return String - period in format "x day(s) y hour(s) z minute(s)"
* or "" if not valid period
*/
public static String parseDayTimePeriod(
long lPeriod
)
{
StringBuffer sbReturn = new StringBuffer();
long lHelp = 0L;
if (lPeriod > 0)
{
lPeriod = lPeriod + DateUtils.ONE_MINUTE - 1;
// we will count each started day as counted day
lHelp = lPeriod / DateUtils.ONE_DAY;
if (lHelp > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" d ");
}
else
{
sbReturn.append(" d ");
}
}
lPeriod = lPeriod % DateUtils.ONE_DAY;
lHelp = lPeriod / DateUtils.ONE_HOUR;
if (lHelp > 0 || sbReturn.length() > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" h ");
}
else
{
sbReturn.append(" h ");
}
}
lPeriod = lPeriod % DateUtils.ONE_HOUR;
lHelp = lPeriod / DateUtils.ONE_MINUTE;
if (lHelp > 0 || sbReturn.length() > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" min");
}
else
{
sbReturn.append(" min");
}
}
}
else
{
sbReturn.append("0 min");
}
return sbReturn.toString();
}
// TODO: For Miro: Remove this code once all the code which referred to these
// was fixed. These should be moved to a GUI related class.
// /**
// * Method for list of timing types.
// *
// * @return List - list of timing types
// */
// public static List getTimingTypes(
// )
// {
// List lstTimingTypes = new ArrayList();
//
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MINUTES),
// "Minute(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_HOURS), "Hour(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_DAYS), "Day(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_WEEKS), "Week(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MONTHS), "Month(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_YEARS), "Year(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NEVER), "Never"));
//
// return lstTimingTypes;
// }
// /**
// * Method for list of timing types with None option.
// *
// * @return List - list of timing types
// */
// public static List getTimingTypesWithNone(
// )
// {
// List lstTimingTypes = new ArrayList();
//
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NONE), "None"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MINUTES),
// "Minute(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_HOURS), "Hour(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_DAYS), "Day(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_WEEKS), "Week(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MONTHS), "Month(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_YEARS), "Year(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NEVER), "Never"));
//
// return lstTimingTypes;
// }
// /**
// * Method for getting string name of the timing type.
// *
// * @param iTimingType - timing type constant
// * @return String - string name of timing type
// */
// public static String getTimingTypeName(
// int iTimingType
// )
// {
// String outTimingTypeName = "Never";
// switch (iTimingType)
// {
// case (DateUtils.TIMING_NEVER):
// {
// outTimingTypeName = "Never";
// break;
// }
// case (DateUtils.TIMING_MINUTES):
// {
// outTimingTypeName = "Minute(s)";
// break;
// }
// case (DateUtils.TIMING_HOURS):
// {
// outTimingTypeName = "Hour(s)";
// break;
// }
// case (DateUtils.TIMING_DAYS):
// {
// outTimingTypeName = "Day(s)";
// break;
// }
// case (DateUtils.TIMING_WEEKS):
// {
// outTimingTypeName = "Week(s)";
// break;
// }
// case (DateUtils.TIMING_MONTHS):
// {
// outTimingTypeName = "Month(s)";
// break;
// }
// case (DateUtils.TIMING_YEARS):
// {
// outTimingTypeName = "Year(s)";
// break;
// }
// case (DateUtils.TIMING_NONE):
// {
// outTimingTypeName = "None";
// break;
// }
// }
//
// return outTimingTypeName;
// }
/**
* Get expiration timestamp from start date, period type and duration. For
* example if the start date is now, period type is hour and duration is 2
* then the result will be timestamp representing now + 2 hours.
*
* @param tsStartDate - start date of period counting
* @param iPeriodType - one of the period type constant TIMING_XXX
* @param iPeriodDuration - period duration, number of time units specified
* by period type
* @return Timestamp - date of period expiration or null if any problem
*/
public static Timestamp getPeriodExpiration(
Timestamp tsStartDate,
int iPeriodType,
int iPeriodDuration
)
{
Timestamp tsReturn = null;
Calendar calHelp;
if (tsStartDate != null && iPeriodDuration > 0
&& iPeriodType > TIMING_NEVER && iPeriodType < TIMING_NONE)
{
calHelp = Calendar.getInstance();
calHelp.setTime(tsStartDate);
switch (iPeriodType)
{
case (TIMING_MINUTES) :
{
calHelp.add(Calendar.MINUTE, iPeriodDuration);
break;
}
case (TIMING_HOURS) :
{
calHelp.add(Calendar.HOUR, iPeriodDuration);
break;
}
case (TIMING_DAYS) :
{
calHelp.add(Calendar.DATE, iPeriodDuration);
break;
}
case (TIMING_WEEKS) :
{
calHelp.add(Calendar.WEEK_OF_YEAR, iPeriodDuration);
break;
}
case (TIMING_MONTHS) :
{
calHelp.add(Calendar.MONTH, iPeriodDuration);
break;
}
case (TIMING_YEARS) :
{
calHelp.add(Calendar.YEAR, iPeriodDuration);
break;
}
default :
{
assert false : "Not supported Timing type " + iPeriodType;
}
}
tsReturn = new Timestamp(calHelp.getTimeInMillis());
}
return tsReturn;
}
/**
* Method to compare time periods
*
* @param iPeriodType1 - first period type, one of the period type constant
* TIMING_XXX
* @param iPeriodDuration1 - first period duration
* @param iPeriodType2 - second period type, one of the period type constant
* TIMING_XXX
* @param iPeriodDuration2 - second period duration
* @return int - 1 - first period is longer
* 0 - periods are same
* -1 - first period is shorter
*/
public static int comparePeriods(
int iPeriodType1,
int iPeriodDuration1,
int iPeriodType2,
int iPeriodDuration2
)
{
int iReturn = 0;
if ((iPeriodType1 != TIMING_NEVER) && (iPeriodType1 != TIMING_NONE)
&& (iPeriodType2 != TIMING_NEVER) && (iPeriodType2 != TIMING_NONE))
{
Timestamp tsTimestamp1 = getPeriodExpiration(
new Timestamp(0), iPeriodType1, iPeriodDuration1);
Timestamp tsTimestamp2 = getPeriodExpiration(
new Timestamp(0), iPeriodType2, iPeriodDuration2);
// TODO: Improve: When would any of these be null?
if ((tsTimestamp1 != null) && (tsTimestamp2 != null))
{
if (tsTimestamp1.after(tsTimestamp2))
{
iReturn = 1;
}
else if (tsTimestamp2.after(tsTimestamp1))
{
iReturn = -1;
}
}
}
else
{
if (iPeriodType1 != iPeriodType2)
{
if (iPeriodType1 == TIMING_NEVER)
{
iReturn = 1;
}
else if (iPeriodType1 == TIMING_NONE)
{
iReturn = -1;
}
else if (iPeriodType2 == TIMING_NEVER)
{
iReturn = -1;
}
else if (iPeriodType2 == TIMING_NONE)
{
iReturn = 1;
}
}
}
return iReturn;
}
/**
* Convert timestamp to string including it"s nanosecond portion so that it
* can be safely stored in variable of web page.
*
* @param tsTimestamp - timestamp to convert
* @return String - text containing time and nanosecond portion of timestamp
*/
public static String getTimestampAsString(
Timestamp tsTimestamp
)
{
StringBuffer sbTimestamp = new StringBuffer();
sbTimestamp.append(tsTimestamp.getTime());
sbTimestamp.append(NANO_SEPARATOR);
sbTimestamp.append(tsTimestamp.getNanos());
return sbTimestamp.toString();
}
/**
* Function returns time string in the form MM:SS.MS from the input specified in miliseconds.
*
* @param lTimeInMiliseconds - time in miliseconds
* @return String - string representation of miliseconds in the form MM:SS.MS
*/
public static String getStringTime(
long lTimeInMiliseconds
)
{
long lTotalMS = lTimeInMiliseconds;
long lMS = lTotalMS % 1000;
long lTotalSecs = lTotalMS / 1000;
long lSecs = lTotalSecs % 60;
long lTotalMins = lTotalSecs / 60;
long lMinutes = lTotalMins % 60;
long lHours = lTotalMins / 60;
StringBuffer sbBuffer = new StringBuffer();
if (lHours > 0)
{
sbBuffer.append(lHours);
sbBuffer.append(":");
sbBuffer.append(lMinutes);
sbBuffer.append(":");
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
}
else if (lMinutes > 0)
{
sbBuffer.append(lMinutes);
sbBuffer.append(":");
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
}
else if (lSecs > 0)
{
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
sbBuffer.append(" seconds");
}
else
{
sbBuffer.append(lMS);
sbBuffer.append(" ms");
}
return sbBuffer.toString();
}
// TODO: For Miro: Remove this code once all the code which referred to these
// was fixed. These should be moved to a GUI or business logic related class.
// /**
// * Method to check if valid period settings
// *
// * @param iPeriod - period length
// * @param iPeriodType - period type
// * @param iPeriodStartType - period start type
// * @param iAttributeId - attribute ID for dynamic period start type
// * @param bPeriodException - period exception flag
// * @param strPeriodName - period name used for exception message
// * @param bAdvancePeriodType - flag if advanced period type (includes also start type)
// * @param bfideException - invalid data exception
// */
// public static void validatePeriod(
// int iPeriod,
// int iPeriodType,
// int iPeriodStartType,
// int iAttributeId,
// boolean bPeriodException,
// String strPeriodName,
// boolean bAdvancePeriodType,
// OSSInvalidDataException messageException
// )
// {
// if ((iPeriod > 0)
// || ((iPeriodType != TIMING_NONE) && (iPeriodType != TIMING_NEVER))
// || (bPeriodException) || (iPeriodStartType != PERIOD_START_TYPE_NONE))
// {
// if (iPeriod <= 0)
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period length for " + strPeriodName + " type."
// );
// }
// else if ((iPeriodType == TIMING_NONE) || (iPeriodType == TIMING_NEVER))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period type for " + strPeriodName + " type."
// );
// }
// else if ((bAdvancePeriodType) && (iPeriodStartType == PERIOD_START_TYPE_NONE))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period start type for " + strPeriodName + " type."
// );
// }
// else if ((bAdvancePeriodType)
// && (iPeriodStartType == PERIOD_START_TYPE_DYNAMIC)
// && (iAttributeId == DataObject.NEW_ID))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period dynamic start attribute for "
// + strPeriodName + " type."
// );
// }
// }
// }
}
Construct java.sql.Timestamp from string
public class DateLabel {
public static void main(String[] args) {
java.util.Date today = new java.util.Date();
java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2005-04-06 09:01:10");
long tsTime1 = ts1.getTime();
long tsTime2 = ts2.getTime();
System.out.println(tsTime1);
System.out.println(tsTime2);
}
}
Convert an Object to a DateTime, without an Exception
/*
* Copyright Javelin Software, All rights reserved.
*/
import java.util.*;
import java.text.*;
/**
* The DateUtil is used as a Utility Class for Dates.
*
* @author Robin Sharp
*/
public class DateUtil
{
public final static long SECOND_MILLIS = 1000;
public final static long MINUTE_MILLIS = SECOND_MILLIS*60;
public final static long HOUR_MILLIS = MINUTE_MILLIS*60;
public final static long DAY_MILLIS = HOUR_MILLIS*24;
public final static long YEAR_MILLIS = DAY_MILLIS*365;
public static DateFormat OUT_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
public static DateFormat OUT_TIME_FORMAT = new SimpleDateFormat("H:mm:ss");
public static DateFormat OUT_DATETIME_FORMAT = new SimpleDateFormat("d/M/yyyy H:mm:ss");
public static DateFormat OUT_TIMESTAMP_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss.SSS");
public static DateFormat IN_DATE_FORMAT = new SimpleDateFormat("d/M/yy");
public static DateFormat IN_TIME_FORMAT = new SimpleDateFormat("H:mm:ss");
public static DateFormat IN_DATETIME_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss");
public static DateFormat IN_TIMESTAMP_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss.SSS");
public static DateFormat DATE_TIME_FORMAT= new SimpleDateFormat( "yyyyMMddkkmmss" );
public static Calendar calendar = new GregorianCalendar();
static
{
IN_DATE_FORMAT.setLenient(false);
IN_TIME_FORMAT.setLenient(false);
IN_DATETIME_FORMAT.setLenient(false);
}
/**
* Create a new DateTime. To the last second. This will not create any
* extra-millis-seconds, which may cause bugs when writing to stores such as
* databases that round milli-seconds up and down.
*/
public static java.util.Date newDateTime()
{
return new java.util.Date( (System.currentTimeMillis()/SECOND_MILLIS)*SECOND_MILLIS);
}
/**
* Create a new Date. To the last day.
*/
public static java.sql.Date newDate()
{
return new java.sql.Date( (System.currentTimeMillis()/DAY_MILLIS)*DAY_MILLIS);
}
/**
* Create a new Time, with no date component.
*/
public static java.sql.Time newTime()
{
return new java.sql.Time( System.currentTimeMillis()%DAY_MILLIS);
}
/**
* Create a new Timestamp.
*/
public static java.sql.Timestamp newTimestamp()
{
return new java.sql.Timestamp( System.currentTimeMillis() );
}
/**
* Get the seconds difference
*/
public static int secondsDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/SECOND_MILLIS) - (earlierDate.getTime()/SECOND_MILLIS));
}
/**
* Get the minutes difference
*/
public static int minutesDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/MINUTE_MILLIS) - (earlierDate.getTime()/MINUTE_MILLIS));
}
/**
* Get the hours difference
*/
public static int hoursDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/HOUR_MILLIS) - (earlierDate.getTime()/HOUR_MILLIS));
}
/**
* Get the days difference
*/
public static int daysDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/DAY_MILLIS) - (earlierDate.getTime()/DAY_MILLIS));
}
/**
* Roll the java.util.Time forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.sql.Time rollTime( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.sql.Time(gc.getTime().getTime());
}
/**
* Roll the java.util.Date forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.util.Date rollDateTime( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.util.Date(gc.getTime().getTime());
}
/**
* Roll the java.sql.Date forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.sql.Date rollDate( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.sql.Date(gc.getTime().getTime());
}
/**
* Roll the years forward or backward.
* @param startDate - The start date
* @param years - Negative to rollbackwards.
*/
public static java.sql.Date rollYears( java.util.Date startDate, int years )
{
return rollDate( startDate, Calendar.YEAR, years );
}
/**
* Roll the days forward or backward.
* @param startDate - The start date
* @param months - Negative to rollbackwards.
*/
public static java.sql.Date rollMonths( java.util.Date startDate, int months )
{
return rollDate( startDate, Calendar.MONTH, months );
}
/**
* Roll the days forward or backward.
* @param startDate - The start date
* @param days - Negative to rollbackwards.
*/
public static java.sql.Date rollDays( java.util.Date startDate, int days )
{
return rollDate( startDate, Calendar.DATE, days );
}
/**
* Checks the day, month and year are equal.
*/
public static boolean dateEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getDate() == d2.getDate() &&
d1.getMonth() == d2.getMonth() &&
d1.getYear() == d2.getYear();
}
/**
* Checks the hour, minute and second are equal.
*/
public static boolean timeEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getHours() == d2.getHours() &&
d1.getMinutes() == d2.getMinutes() &&
d1.getSeconds() == d2.getSeconds();
}
/**
* Checks the second, hour, month, day, month and year are equal.
*/
public static boolean dateTimeEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getDate() == d2.getDate() &&
d1.getMonth() == d2.getMonth() &&
d1.getYear() == d2.getYear() &&
d1.getHours() == d2.getHours() &&
d1.getMinutes() == d2.getMinutes() &&
d1.getSeconds() == d2.getSeconds();
}
/**
* Convert an Object of type Classs to an Object.
*/
public static Object toObject( Class clazz, Object value ) throws ParseException
{
if( value == null ) return null;
if( clazz == null ) return value;
if( java.sql.Date.class.isAssignableFrom( clazz ) ) return toDate( value );
if( java.sql.Time.class.isAssignableFrom( clazz ) ) return toTime( value );
if( java.sql.Timestamp.class.isAssignableFrom( clazz ) ) return toTimestamp( value );
if( java.util.Date.class.isAssignableFrom( clazz ) ) return toDateTime( value );
return value;
}
/**
* Convert an Object to a DateTime, without an Exception
*/
public static java.util.Date getDateTime( Object value )
{
try
{
return toDateTime( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a DateTime.
*/
public static java.util.Date toDateTime( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.util.Date ) return (java.util.Date)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return IN_DATETIME_FORMAT.parse( (String)value );
}
return IN_DATETIME_FORMAT.parse( value.toString() );
}
/**
* Convert an Object to a Date, without an Exception
*/
public static java.sql.Date getDate( Object value )
{
try
{
return toDate( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Date.
*/
public static java.sql.Date toDate( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Date ) return (java.sql.Date)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Date( IN_DATE_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Date( IN_DATE_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Convert an Object to a Time, without an Exception
*/
public static java.sql.Time getTime( Object value )
{
try
{
return toTime( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Time.
*/
public static java.sql.Time toTime( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Time ) return (java.sql.Time)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Time( IN_TIME_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Time( IN_TIME_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Convert an Object to a Timestamp, without an Exception
*/
public static java.sql.Timestamp getTimestamp( Object value )
{
try
{
return toTimestamp( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Timestamp.
*/
public static java.sql.Timestamp toTimestamp( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Timestamp ) return (java.sql.Timestamp)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Timestamp( IN_TIMESTAMP_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Timestamp( IN_TIMESTAMP_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Tells you if the date part of a datetime is in a certain time range.
*/
public static boolean isTimeInRange( java.sql.Time start, java.sql.Time end, java.util.Date d )
{
d=new java.sql.Time(d.getHours(),d.getMinutes(),d.getSeconds());
if (start==null || end==null)
{
return false;
}
if (start.before(end)&&(!(d.after(start)&&d.before(end))))
{
return false;
}
if (end.before(start)&&(!(d.after(end)||d.before(start))))
{
return false;
}
return true;
}
public static int getYear( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.YEAR );
}
public static int getMonth( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MONTH );
}
public static int getDate( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.DATE );
}
public static int getHour( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.HOUR );
}
public static int getMinute( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MINUTE );
}
public static int getSeconds( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.SECOND );
}
public static int getMillisecond( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MILLISECOND );
}
/**
* Convert an Object to a String using Dates
*/
public static String toString( Object date )
{
if( date == null ) return null;
if( java.sql.Timestamp.class.isAssignableFrom( date.getClass() ) )
{
return OUT_TIMESTAMP_FORMAT.format( date );
}
if( java.sql.Time.class.isAssignableFrom( date.getClass() ) )
{
return OUT_TIME_FORMAT.format( date );
}
if( java.sql.Date.class.isAssignableFrom( date.getClass() ) )
{
return OUT_DATE_FORMAT.format( date );
}
if( java.util.Date.class.isAssignableFrom( date.getClass() ) )
{
return OUT_DATETIME_FORMAT.format( date );
}
throw new IllegalArgumentException( "Unsupported type " + date.getClass() );
}
}
Convert an Object to a java.sql.Time
/*
* Copyright Javelin Software, All rights reserved.
*/
import java.util.*;
import java.text.*;
/**
* The DateUtil is used as a Utility Class for Dates.
*
* @author Robin Sharp
*/
public class DateUtil
{
public final static long SECOND_MILLIS = 1000;
public final static long MINUTE_MILLIS = SECOND_MILLIS*60;
public final static long HOUR_MILLIS = MINUTE_MILLIS*60;
public final static long DAY_MILLIS = HOUR_MILLIS*24;
public final static long YEAR_MILLIS = DAY_MILLIS*365;
public static DateFormat OUT_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
public static DateFormat OUT_TIME_FORMAT = new SimpleDateFormat("H:mm:ss");
public static DateFormat OUT_DATETIME_FORMAT = new SimpleDateFormat("d/M/yyyy H:mm:ss");
public static DateFormat OUT_TIMESTAMP_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss.SSS");
public static DateFormat IN_DATE_FORMAT = new SimpleDateFormat("d/M/yy");
public static DateFormat IN_TIME_FORMAT = new SimpleDateFormat("H:mm:ss");
public static DateFormat IN_DATETIME_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss");
public static DateFormat IN_TIMESTAMP_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss.SSS");
public static DateFormat DATE_TIME_FORMAT= new SimpleDateFormat( "yyyyMMddkkmmss" );
public static Calendar calendar = new GregorianCalendar();
static
{
IN_DATE_FORMAT.setLenient(false);
IN_TIME_FORMAT.setLenient(false);
IN_DATETIME_FORMAT.setLenient(false);
}
/**
* Create a new DateTime. To the last second. This will not create any
* extra-millis-seconds, which may cause bugs when writing to stores such as
* databases that round milli-seconds up and down.
*/
public static java.util.Date newDateTime()
{
return new java.util.Date( (System.currentTimeMillis()/SECOND_MILLIS)*SECOND_MILLIS);
}
/**
* Create a new Date. To the last day.
*/
public static java.sql.Date newDate()
{
return new java.sql.Date( (System.currentTimeMillis()/DAY_MILLIS)*DAY_MILLIS);
}
/**
* Create a new Time, with no date component.
*/
public static java.sql.Time newTime()
{
return new java.sql.Time( System.currentTimeMillis()%DAY_MILLIS);
}
/**
* Create a new Timestamp.
*/
public static java.sql.Timestamp newTimestamp()
{
return new java.sql.Timestamp( System.currentTimeMillis() );
}
/**
* Get the seconds difference
*/
public static int secondsDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/SECOND_MILLIS) - (earlierDate.getTime()/SECOND_MILLIS));
}
/**
* Get the minutes difference
*/
public static int minutesDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/MINUTE_MILLIS) - (earlierDate.getTime()/MINUTE_MILLIS));
}
/**
* Get the hours difference
*/
public static int hoursDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/HOUR_MILLIS) - (earlierDate.getTime()/HOUR_MILLIS));
}
/**
* Get the days difference
*/
public static int daysDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/DAY_MILLIS) - (earlierDate.getTime()/DAY_MILLIS));
}
/**
* Roll the java.util.Time forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.sql.Time rollTime( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.sql.Time(gc.getTime().getTime());
}
/**
* Roll the java.util.Date forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.util.Date rollDateTime( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.util.Date(gc.getTime().getTime());
}
/**
* Roll the java.sql.Date forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.sql.Date rollDate( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.sql.Date(gc.getTime().getTime());
}
/**
* Roll the years forward or backward.
* @param startDate - The start date
* @param years - Negative to rollbackwards.
*/
public static java.sql.Date rollYears( java.util.Date startDate, int years )
{
return rollDate( startDate, Calendar.YEAR, years );
}
/**
* Roll the days forward or backward.
* @param startDate - The start date
* @param months - Negative to rollbackwards.
*/
public static java.sql.Date rollMonths( java.util.Date startDate, int months )
{
return rollDate( startDate, Calendar.MONTH, months );
}
/**
* Roll the days forward or backward.
* @param startDate - The start date
* @param days - Negative to rollbackwards.
*/
public static java.sql.Date rollDays( java.util.Date startDate, int days )
{
return rollDate( startDate, Calendar.DATE, days );
}
/**
* Checks the day, month and year are equal.
*/
public static boolean dateEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getDate() == d2.getDate() &&
d1.getMonth() == d2.getMonth() &&
d1.getYear() == d2.getYear();
}
/**
* Checks the hour, minute and second are equal.
*/
public static boolean timeEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getHours() == d2.getHours() &&
d1.getMinutes() == d2.getMinutes() &&
d1.getSeconds() == d2.getSeconds();
}
/**
* Checks the second, hour, month, day, month and year are equal.
*/
public static boolean dateTimeEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getDate() == d2.getDate() &&
d1.getMonth() == d2.getMonth() &&
d1.getYear() == d2.getYear() &&
d1.getHours() == d2.getHours() &&
d1.getMinutes() == d2.getMinutes() &&
d1.getSeconds() == d2.getSeconds();
}
/**
* Convert an Object of type Classs to an Object.
*/
public static Object toObject( Class clazz, Object value ) throws ParseException
{
if( value == null ) return null;
if( clazz == null ) return value;
if( java.sql.Date.class.isAssignableFrom( clazz ) ) return toDate( value );
if( java.sql.Time.class.isAssignableFrom( clazz ) ) return toTime( value );
if( java.sql.Timestamp.class.isAssignableFrom( clazz ) ) return toTimestamp( value );
if( java.util.Date.class.isAssignableFrom( clazz ) ) return toDateTime( value );
return value;
}
/**
* Convert an Object to a DateTime, without an Exception
*/
public static java.util.Date getDateTime( Object value )
{
try
{
return toDateTime( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a DateTime.
*/
public static java.util.Date toDateTime( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.util.Date ) return (java.util.Date)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return IN_DATETIME_FORMAT.parse( (String)value );
}
return IN_DATETIME_FORMAT.parse( value.toString() );
}
/**
* Convert an Object to a Date, without an Exception
*/
public static java.sql.Date getDate( Object value )
{
try
{
return toDate( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Date.
*/
public static java.sql.Date toDate( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Date ) return (java.sql.Date)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Date( IN_DATE_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Date( IN_DATE_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Convert an Object to a Time, without an Exception
*/
public static java.sql.Time getTime( Object value )
{
try
{
return toTime( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Time.
*/
public static java.sql.Time toTime( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Time ) return (java.sql.Time)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Time( IN_TIME_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Time( IN_TIME_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Convert an Object to a Timestamp, without an Exception
*/
public static java.sql.Timestamp getTimestamp( Object value )
{
try
{
return toTimestamp( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Timestamp.
*/
public static java.sql.Timestamp toTimestamp( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Timestamp ) return (java.sql.Timestamp)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Timestamp( IN_TIMESTAMP_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Timestamp( IN_TIMESTAMP_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Tells you if the date part of a datetime is in a certain time range.
*/
public static boolean isTimeInRange( java.sql.Time start, java.sql.Time end, java.util.Date d )
{
d=new java.sql.Time(d.getHours(),d.getMinutes(),d.getSeconds());
if (start==null || end==null)
{
return false;
}
if (start.before(end)&&(!(d.after(start)&&d.before(end))))
{
return false;
}
if (end.before(start)&&(!(d.after(end)||d.before(start))))
{
return false;
}
return true;
}
public static int getYear( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.YEAR );
}
public static int getMonth( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MONTH );
}
public static int getDate( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.DATE );
}
public static int getHour( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.HOUR );
}
public static int getMinute( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MINUTE );
}
public static int getSeconds( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.SECOND );
}
public static int getMillisecond( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MILLISECOND );
}
/**
* Convert an Object to a String using Dates
*/
public static String toString( Object date )
{
if( date == null ) return null;
if( java.sql.Timestamp.class.isAssignableFrom( date.getClass() ) )
{
return OUT_TIMESTAMP_FORMAT.format( date );
}
if( java.sql.Time.class.isAssignableFrom( date.getClass() ) )
{
return OUT_TIME_FORMAT.format( date );
}
if( java.sql.Date.class.isAssignableFrom( date.getClass() ) )
{
return OUT_DATE_FORMAT.format( date );
}
if( java.util.Date.class.isAssignableFrom( date.getClass() ) )
{
return OUT_DATETIME_FORMAT.format( date );
}
throw new IllegalArgumentException( "Unsupported type " + date.getClass() );
}
}
Convert an Object to a Timestamp, without an Exception
/*
* Copyright Javelin Software, All rights reserved.
*/
import java.util.*;
import java.text.*;
/**
* The DateUtil is used as a Utility Class for Dates.
*
* @author Robin Sharp
*/
public class DateUtil
{
public final static long SECOND_MILLIS = 1000;
public final static long MINUTE_MILLIS = SECOND_MILLIS*60;
public final static long HOUR_MILLIS = MINUTE_MILLIS*60;
public final static long DAY_MILLIS = HOUR_MILLIS*24;
public final static long YEAR_MILLIS = DAY_MILLIS*365;
public static DateFormat OUT_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
public static DateFormat OUT_TIME_FORMAT = new SimpleDateFormat("H:mm:ss");
public static DateFormat OUT_DATETIME_FORMAT = new SimpleDateFormat("d/M/yyyy H:mm:ss");
public static DateFormat OUT_TIMESTAMP_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss.SSS");
public static DateFormat IN_DATE_FORMAT = new SimpleDateFormat("d/M/yy");
public static DateFormat IN_TIME_FORMAT = new SimpleDateFormat("H:mm:ss");
public static DateFormat IN_DATETIME_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss");
public static DateFormat IN_TIMESTAMP_FORMAT = new SimpleDateFormat("d/M/yy H:mm:ss.SSS");
public static DateFormat DATE_TIME_FORMAT= new SimpleDateFormat( "yyyyMMddkkmmss" );
public static Calendar calendar = new GregorianCalendar();
static
{
IN_DATE_FORMAT.setLenient(false);
IN_TIME_FORMAT.setLenient(false);
IN_DATETIME_FORMAT.setLenient(false);
}
/**
* Create a new DateTime. To the last second. This will not create any
* extra-millis-seconds, which may cause bugs when writing to stores such as
* databases that round milli-seconds up and down.
*/
public static java.util.Date newDateTime()
{
return new java.util.Date( (System.currentTimeMillis()/SECOND_MILLIS)*SECOND_MILLIS);
}
/**
* Create a new Date. To the last day.
*/
public static java.sql.Date newDate()
{
return new java.sql.Date( (System.currentTimeMillis()/DAY_MILLIS)*DAY_MILLIS);
}
/**
* Create a new Time, with no date component.
*/
public static java.sql.Time newTime()
{
return new java.sql.Time( System.currentTimeMillis()%DAY_MILLIS);
}
/**
* Create a new Timestamp.
*/
public static java.sql.Timestamp newTimestamp()
{
return new java.sql.Timestamp( System.currentTimeMillis() );
}
/**
* Get the seconds difference
*/
public static int secondsDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/SECOND_MILLIS) - (earlierDate.getTime()/SECOND_MILLIS));
}
/**
* Get the minutes difference
*/
public static int minutesDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/MINUTE_MILLIS) - (earlierDate.getTime()/MINUTE_MILLIS));
}
/**
* Get the hours difference
*/
public static int hoursDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/HOUR_MILLIS) - (earlierDate.getTime()/HOUR_MILLIS));
}
/**
* Get the days difference
*/
public static int daysDiff( Date earlierDate, Date laterDate )
{
if( earlierDate == null || laterDate == null ) return 0;
return (int)((laterDate.getTime()/DAY_MILLIS) - (earlierDate.getTime()/DAY_MILLIS));
}
/**
* Roll the java.util.Time forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.sql.Time rollTime( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.sql.Time(gc.getTime().getTime());
}
/**
* Roll the java.util.Date forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.util.Date rollDateTime( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.util.Date(gc.getTime().getTime());
}
/**
* Roll the java.sql.Date forward or backward.
* @param startDate - The start date
* @period Calendar.YEAR etc
* @param amount - Negative to rollbackwards.
*/
public static java.sql.Date rollDate( java.util.Date startDate, int period, int amount )
{
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(startDate);
gc.add(period, amount);
return new java.sql.Date(gc.getTime().getTime());
}
/**
* Roll the years forward or backward.
* @param startDate - The start date
* @param years - Negative to rollbackwards.
*/
public static java.sql.Date rollYears( java.util.Date startDate, int years )
{
return rollDate( startDate, Calendar.YEAR, years );
}
/**
* Roll the days forward or backward.
* @param startDate - The start date
* @param months - Negative to rollbackwards.
*/
public static java.sql.Date rollMonths( java.util.Date startDate, int months )
{
return rollDate( startDate, Calendar.MONTH, months );
}
/**
* Roll the days forward or backward.
* @param startDate - The start date
* @param days - Negative to rollbackwards.
*/
public static java.sql.Date rollDays( java.util.Date startDate, int days )
{
return rollDate( startDate, Calendar.DATE, days );
}
/**
* Checks the day, month and year are equal.
*/
public static boolean dateEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getDate() == d2.getDate() &&
d1.getMonth() == d2.getMonth() &&
d1.getYear() == d2.getYear();
}
/**
* Checks the hour, minute and second are equal.
*/
public static boolean timeEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getHours() == d2.getHours() &&
d1.getMinutes() == d2.getMinutes() &&
d1.getSeconds() == d2.getSeconds();
}
/**
* Checks the second, hour, month, day, month and year are equal.
*/
public static boolean dateTimeEquals( java.util.Date d1, java.util.Date d2 )
{
if( d1 == null || d2 == null ) return false;
return d1.getDate() == d2.getDate() &&
d1.getMonth() == d2.getMonth() &&
d1.getYear() == d2.getYear() &&
d1.getHours() == d2.getHours() &&
d1.getMinutes() == d2.getMinutes() &&
d1.getSeconds() == d2.getSeconds();
}
/**
* Convert an Object of type Classs to an Object.
*/
public static Object toObject( Class clazz, Object value ) throws ParseException
{
if( value == null ) return null;
if( clazz == null ) return value;
if( java.sql.Date.class.isAssignableFrom( clazz ) ) return toDate( value );
if( java.sql.Time.class.isAssignableFrom( clazz ) ) return toTime( value );
if( java.sql.Timestamp.class.isAssignableFrom( clazz ) ) return toTimestamp( value );
if( java.util.Date.class.isAssignableFrom( clazz ) ) return toDateTime( value );
return value;
}
/**
* Convert an Object to a DateTime, without an Exception
*/
public static java.util.Date getDateTime( Object value )
{
try
{
return toDateTime( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a DateTime.
*/
public static java.util.Date toDateTime( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.util.Date ) return (java.util.Date)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return IN_DATETIME_FORMAT.parse( (String)value );
}
return IN_DATETIME_FORMAT.parse( value.toString() );
}
/**
* Convert an Object to a Date, without an Exception
*/
public static java.sql.Date getDate( Object value )
{
try
{
return toDate( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Date.
*/
public static java.sql.Date toDate( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Date ) return (java.sql.Date)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Date( IN_DATE_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Date( IN_DATE_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Convert an Object to a Time, without an Exception
*/
public static java.sql.Time getTime( Object value )
{
try
{
return toTime( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Time.
*/
public static java.sql.Time toTime( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Time ) return (java.sql.Time)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Time( IN_TIME_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Time( IN_TIME_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Convert an Object to a Timestamp, without an Exception
*/
public static java.sql.Timestamp getTimestamp( Object value )
{
try
{
return toTimestamp( value );
}
catch( ParseException pe )
{
pe.printStackTrace();
return null;
}
}
/**
* Convert an Object to a Timestamp.
*/
public static java.sql.Timestamp toTimestamp( Object value ) throws ParseException
{
if( value == null ) return null;
if( value instanceof java.sql.Timestamp ) return (java.sql.Timestamp)value;
if( value instanceof String )
{
if( "".equals( (String)value ) ) return null;
return new java.sql.Timestamp( IN_TIMESTAMP_FORMAT.parse( (String)value ).getTime() );
}
return new java.sql.Timestamp( IN_TIMESTAMP_FORMAT.parse( value.toString() ).getTime() );
}
/**
* Tells you if the date part of a datetime is in a certain time range.
*/
public static boolean isTimeInRange( java.sql.Time start, java.sql.Time end, java.util.Date d )
{
d=new java.sql.Time(d.getHours(),d.getMinutes(),d.getSeconds());
if (start==null || end==null)
{
return false;
}
if (start.before(end)&&(!(d.after(start)&&d.before(end))))
{
return false;
}
if (end.before(start)&&(!(d.after(end)||d.before(start))))
{
return false;
}
return true;
}
public static int getYear( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.YEAR );
}
public static int getMonth( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MONTH );
}
public static int getDate( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.DATE );
}
public static int getHour( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.HOUR );
}
public static int getMinute( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MINUTE );
}
public static int getSeconds( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.SECOND );
}
public static int getMillisecond( Date date )
{
calendar.setTime( date );
return calendar.get( Calendar.MILLISECOND );
}
/**
* Convert an Object to a String using Dates
*/
public static String toString( Object date )
{
if( date == null ) return null;
if( java.sql.Timestamp.class.isAssignableFrom( date.getClass() ) )
{
return OUT_TIMESTAMP_FORMAT.format( date );
}
if( java.sql.Time.class.isAssignableFrom( date.getClass() ) )
{
return OUT_TIME_FORMAT.format( date );
}
if( java.sql.Date.class.isAssignableFrom( date.getClass() ) )
{
return OUT_DATE_FORMAT.format( date );
}
if( java.util.Date.class.isAssignableFrom( date.getClass() ) )
{
return OUT_DATETIME_FORMAT.format( date );
}
throw new IllegalArgumentException( "Unsupported type " + date.getClass() );
}
}
Convert into java.sql.Time (or into java.util.Calendar)
import java.sql.Time;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Provides methods helpful in making object conversions not provided for by the
* Sun or MyFaces distributions.
*
* @author
*
*/
public class ConversionUtil {
/**
* convert into java.sql.Time (or into java.util.Calendar
*
* @param date
* The date containing the time.
* @param am
* Whether this should be am (true) or pm (false)
* @return
*/
public static Time convertDateToTime(Date date, boolean am) {
if (date == null) {
return null;
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
if (am) {
// Check to make sure that the hours are indeed am hours
if (hourOfDay > 11) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay - 12);
date.setTime(cal.getTimeInMillis());
}
} else {
// Check to make sure that the hours are indeed pm hours
if (cal.get(Calendar.HOUR_OF_DAY) < 11) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay + 12);
date.setTime(cal.getTimeInMillis());
}
}
return new Time(date.getTime());
}
}
Convert java.sql.Timestamp to long for easy compare
public class DateLabel {
public static void main(String[] args) {
java.util.Date today = new java.util.Date();
java.sql.Timestamp ts1 = new java.sql.Timestamp(today.getTime());
java.sql.Timestamp ts2 = java.sql.Timestamp.valueOf("2005-04-06 09:01:10");
long tsTime1 = ts1.getTime();
long tsTime2 = ts2.getTime();
System.out.println(tsTime1);
System.out.println(tsTime2);
}
}
convert Strings to Dates and Timestamps and vice versa.
/*
* Copyright 2004 Blandware (http://www.blandware.ru)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.HashMap;
/**
* <p>Date Utility Class.
* This is used to convert Strings to Dates and Timestamps and vice versa.
* </p>
* <p>
* @version $Revision: 1.12 $ $Date: 2008/01/14 10:59:49 $
*/
public class DateUtil {
protected static String datePattern = "MM/dd/yyyy";
protected static HashMap patterns = new HashMap();
/**
* Formats given date according to specified locale and date style
*
* @param date Date to convert
* @param locale Locale to use for formatting date
* @param dateStyle Date style
* @return String representation of date according to given locale and date style
* @see java.text.DateFormat
*/
public static String formatDate(Date date, Locale locale, int dateStyle) {
DateFormat formatter = DateFormat.getDateInstance(dateStyle, locale);
return formatter.format(date);
}
/**
* Formats given date according to specified locale and <code>DateFormat.MEDIUM</code> style
*
* @param date Date to convert
* @param locale Locale to use for formatting date
* @return String representation of date according to given locale and <code>DateFormat.MEDIUM</code> style
* @see java.text.DateFormat
* @see java.text.DateFormat#MEDIUM
*/
public static String formatDate(Date date, Locale locale) {
return formatDate(date, locale, DateFormat.MEDIUM);
}
/**
* Parses given string according to specified locale and date style
*
* @param source Source string to parse date from
* @param locale Locale to use for parsing date
* @param dateStyle Date style
* @return Date object corresponding to representation given in source string
* @throws ParseException if given string could not be properly parsed according to given locale and style
* @see java.text.DateFormat
*/
public static Date parseDate(String source, Locale locale, int dateStyle) throws ParseException {
DateFormat formatter = DateFormat.getDateInstance(dateStyle, locale);
return formatter.parse(source);
}
/**
* Parses given string according to specified locale and <code>DateFormat.MEDIUM</code> style
*
* @param source Source string to parse date from
* @param locale Locale to use for parsing date
* @return Date object corresponding to representation given in source string
* @throws ParseException if given string could not be properly parsed according to given locale and <code>DateFormat.MEDIUM</code> style
* @see java.text.DateFormat
* @see java.text.DateFormat#MEDIUM
*/
public static Date parseDate(String source, Locale locale) throws ParseException {
return parseDate(source, locale, DateFormat.MEDIUM);
}
/**
* Formats given time according to specified locale and time style
*
* @param time Time to convert
* @param locale Locale to use for formatting time
* @param timeStyle Time style
* @return String representation of time according to given locale and time style
* @see java.text.DateFormat
*/
public static String formatTime(Date time, Locale locale, int timeStyle) {
DateFormat formatter = DateFormat.getTimeInstance(timeStyle, locale);
return formatter.format(time);
}
/**
* Formats given time according to specified locale and <code>DateFormat.MEDIUM</code> style
*
* @param time Time to convert
* @param locale Locale to use for formatting time
* @return String representation of time according to given locale and <code>DateFormat.MEDIUM</code> style
* @see java.text.DateFormat
* @see java.text.DateFormat#MEDIUM
*/
public static String formatTime(Date time, Locale locale) {
return formatTime(time, locale, DateFormat.MEDIUM);
}
/**
* Parses given string according to specified locale and time style
*
* @param source Source string to parse time from
* @param locale Locale to use for parsing time
* @param timeStyle Time style
* @return Time object corresponding to representation given in source string
* @throws ParseException if given string could not be properly parsed according to given locale and style
* @see java.text.DateFormat
*/
public static Date parseTime(String source, Locale locale, int timeStyle) throws ParseException {
DateFormat formatter = DateFormat.getTimeInstance(timeStyle, locale);
return formatter.parse(source);
}
/**
* Parses given string according to specified locale and <code>DateFormat.MEDIUM</code> style
*
* @param source Source string to parse time from
* @param locale Locale to use for parsing time
* @return Time object corresponding to representation given in source string
* @throws ParseException if given string could not be properly parsed according to given locale and <code>DateFormat.MEDIUM</code> style
* @see java.text.DateFormat
* @see java.text.DateFormat#MEDIUM
*/
public static Date parseTime(String source, Locale locale) throws ParseException {
return parseTime(source, locale, DateFormat.MEDIUM);
}
/**
* Formats given date and time according to specified locale and date style
*
* @param date Date object to convert
* @param locale Locale to use for formatting date and time
* @param dateStyle Date style
* @param timeStyle Time style
* @return String representation of date and time according to given locale and date style
* @see java.text.DateFormat
*/
public static String formatDateTime(Date date, Locale locale, int dateStyle, int timeStyle) {
DateFormat formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
return formatter.format(date);
}
/**
* Formats given date and time according to specified locale and <code>DateFormat.MEDIUM</code> style
*
* @param date Date object to convert
* @param locale Locale to use for formatting date and time
* @return String representation of date and time according to given locale and <code>DateFormat.MEDIUM</code> style
* @see java.text.DateFormat
* @see java.text.DateFormat#MEDIUM
*/
public static String formatDateTime(Date date, Locale locale) {
return formatDateTime(date, locale, DateFormat.MEDIUM, DateFormat.MEDIUM);
}
/**
* Parses given string according to specified locale and date and time styles
*
* @param source Source string to parse date and time from
* @param locale Locale to use for parsing date and time
* @param dateStyle Date style
* @param timeStyle Time style
* @return Date object corresponding to representation given in source string
* @throws ParseException if given string could not be properly parsed according to given locale and style
* @see java.text.DateFormat
*/
public static Date parseDateTime(String source, Locale locale, int dateStyle, int timeStyle) throws ParseException {
DateFormat formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
return formatter.parse(source);
}
/**
* Parses given string according to specified locale and <code>DateFormat.MEDIUM</code> style
*
* @param source Source string to parse date and time from
* @param locale Locale to use for parsing date and time
* @return Date object corresponding to representation given in source string
* @throws ParseException if given string could not be properly parsed according to given locale and <code>DateFormat.MEDIUM</code> style
* @see java.text.DateFormat
* @see java.text.DateFormat#MEDIUM
*/
public static Date parseDateTime(String source, Locale locale) throws ParseException {
return parseDateTime(source, locale, DateFormat.MEDIUM, DateFormat.MEDIUM);
}
/**
* Formats given Date object according to specified locale and a given
* pattern.
*
* @param date Date object to convert
* @param locale Locale to use for formatting
* @param pattern Pattern to use
* @return String representation of date and time according to given locale and <code>DateFormat.MEDIUM</code> style
* @see java.text.SimpleDateFormat
*/
public static String format(Date date, Locale locale, String pattern) {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
return formatter.format(date);
}
/**
* Parses given string according to specified locale and a given pattern.
*
* @param source Source string to parse date and time from
* @param locale Locale to use for parsing date and time
* @param pattern Pattern to use
* @return Date object corresponding to representation given in source
* string
* @throws ParseException if given string could not be properly parsed
* according to given locale and pattern
* @see java.text.SimpleDateFormat
*/
public static Date parse(String source, Locale locale, String pattern)
throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
return formatter.parse(source);
}
/**
* Returns default datePattern (MM/dd/yyyy)
*
* @return a string representing the date pattern on the UI
*/
public static String getDatePattern() {
return datePattern;
}
/**
* Gets date pattern without time symbols according to given locale and date style
*
* @param locale Locale to searh pattern for
* @param dateStyle Date style to search pattern by
* @return Date pattern without time symbols
*/
public static String getDatePattern(Locale locale, int dateStyle) {
String id = locale.toString() + "+" + dateStyle;
String pattern = (String) patterns.get(id);
if (pattern == null) {
SimpleDateFormat format = (SimpleDateFormat) DateFormat.getDateInstance(dateStyle, locale);
pattern = format.toPattern();
patterns.put(id, pattern);
}
return pattern;
}
/**
* Gets date pattern without time symbols according to given locale in MEDIUM style
*
* @param locale Locale to searh pattern for
* @return Date pattern without time symbols in medium format
* @see java.text.DateFormat#MEDIUM
*/
public static String getDatePattern(Locale locale) {
return getDatePattern(locale, DateFormat.MEDIUM);
}
}
Get Date From MySql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class GetDateFromMySql {
public static Connection getMySQLConnection() throws Exception {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost/databaseName";
String username = "root";
String password = "root";
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
}
public static void main(String args[]) {
ResultSet rs = null;
Connection conn = null;
Statement stmt = null;
try {
conn = getMySQLConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select timeCol, dateCol, dateTimeCol from dateTimeTable");
while (rs.next()) {
java.sql.Time dbSqlTime = rs.getTime(1);
java.sql.Date dbSqlDate = rs.getDate(2);
java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
System.out.println("dbSqlTime=" + dbSqlTime);
System.out.println("dbSqlDate=" + dbSqlDate);
System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);
java.util.Date dbSqlTimeConverted = new java.util.Date(dbSqlTime.getTime());
java.util.Date dbSqlDateConverted = new java.util.Date(dbSqlDate.getTime());
System.out.println("in standard date");
System.out.println(dbSqlTimeConverted);
System.out.println(dbSqlDateConverted);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Get date from Oracle
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GetDateFromOracle {
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
Class.forName(driver);
return DriverManager.getConnection(url, "name", "password");
}
public static void main(String args[]) {
String GET_RECORD = "select date_column, time_column, "
+ "timestamp_column from TestDates where id = ?";
ResultSet rs = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(GET_RECORD);
pstmt.setString(1, "0001");
rs = pstmt.executeQuery();
while (rs.next()) {
java.sql.Date dbSqlDate = rs.getDate(1);
java.sql.Time dbSqlTime = rs.getTime(2);
java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
System.out.println("dbSqlDate=" + dbSqlDate);
System.out.println("dbSqlTime=" + dbSqlTime);
System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Get java.sql.Timestamp fro current time
public class DateLabel {
public static void main(String[] args) {
java.util.Date today = new java.util.Date();
System.out.println(new java.sql.Timestamp(today.getTime()));
}
}
Insert Date, time and date time data to Oracle
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class InsertDateToOracle {
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
Class.forName(driver);
return DriverManager.getConnection(url, "userName", "password");
}
public static void main(String args[])throws Exception {
String INSERT_RECORD = "insert into TestDates(id, date_column, "
+ "time_column, timestamp_column) values(?, ?, ?, ?)";
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(INSERT_RECORD);
pstmt.setString(1, "001");
java.util.Date date = new java.util.Date();
long t = date.getTime();
java.sql.Date sqlDate = new java.sql.Date(t);
java.sql.Time sqlTime = new java.sql.Time(t);
java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);
System.out.println("sqlDate=" + sqlDate);
System.out.println("sqlTime=" + sqlTime);
System.out.println("sqlTimestamp=" + sqlTimestamp);
pstmt.setDate(2, sqlDate);
pstmt.setTime(3, sqlTime);
pstmt.setTimestamp(4, sqlTimestamp);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Failed to insert the record.");
} finally {
pstmt.close();
conn.close();
}
}
}
Parse date and time
/**
* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
*
* Project: OpenSubsystems
*
* $Id: DateUtils.java,v 1.7 2007/01/07 06:14:00 bastafidli Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Collection of useful utilities to work with dates.
*
* @version $Id: DateUtils.java,v 1.7 2007/01/07 06:14:00 bastafidli Exp $
* @author Miro Halas
* @code.reviewer Miro Halas
* @code.reviewed 1.5 2005/09/13 13:23:15 bastafidli
*/
public final class DateUtils
{
// Constants ////////////////////////////////////////////////////////////////
/**
* One second in milliseconds.
*/
public static final long ONE_SECOND = 1000L;
/**
* One minute in milliseconds.
*/
public static final long ONE_MINUTE = ONE_SECOND * 60L;
/**
* One hour in milliseconds.
*/
public static final long ONE_HOUR = ONE_MINUTE * 60L;
/**
* One day in milliseconds.
*/
public static final long ONE_DAY = ONE_HOUR * 24L;
/**
* Separator we used to separate time from the nanosecond portion of the
* timestamp when converted to string.
*/
public static final char NANO_SEPARATOR = ":";
/**
* Constant for timing type
*/
public static final int TIMING_NEVER = 0;
/**
* Constant for timing type
*/
public static final int TIMING_MINUTES = 1;
/**
* Constant for timing type
*/
public static final int TIMING_HOURS = 2;
/**
* Constant for timing type
*/
public static final int TIMING_DAYS = 3;
/**
* Constant for timing type
*/
public static final int TIMING_WEEKS = 4;
/**
* Constant for timing type
*/
public static final int TIMING_MONTHS = 5;
/**
* Constant for timing type
*/
public static final int TIMING_YEARS = 6;
/**
* Constant for timing type
*/
public static final int TIMING_NONE = 7;
/**
* Constant for current date code used in date/time formulas
*/
public static final String CURRENT_DATE_CODE = "now";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char YEAR_CODE = "y";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char MONTH_CODE = "M";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char WEEK_CODE = "w";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char DAY_CODE = "d";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char HOUR_CODE = "h";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char MINUTE_CODE = "m";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char SECOND_CODE = "s";
/**
* constant for date type DATE
*/
public static final int DATE_TYPE_DATE = 1;
/**
* constant for date type TIME
*/
public static final int DATE_TYPE_TIME = 2;
/**
* constant for date type DATETIME
*/
public static final int DATE_TYPE_DATETIME = 3;
// Constants for period start types /////////////////////////////////////////
// TODO: For Miro: Remove this code once all the code which referred to these
// constants was fixed
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_NONE = 0;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_CREATION = 1;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_COMPLETION = 2;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_APPROVAL = 3;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_ACTIVATION = 4;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_INACTIVATION = 5;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_DYNAMIC = 6;
//
// /**
// * constant for period type code
// */
// public static final int PERIOD_TYPE_CODE = 99;
//
// /**
// * constant for period type object
// */
// public static final Integer PERIOD_TYPE_OBJ = new Integer(PERIOD_TYPE_CODE);
// Cached variables /////////////////////////////////////////////////////////
/**
* static SimpleDateFormat for date format to display on UI and in messages.
*/
public static final SimpleDateFormat DATE_FORMAT
= (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT);
/**
* static SimpleDateFormat for time format to display on UI and in messages.
*/
public static final SimpleDateFormat TIME_FORMAT
= (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM);
/**
* static SimpleDateFormat for datetime format to display on UI and in messages.
*/
public static final SimpleDateFormat DATETIME_FORMAT
= (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM);
/**
* static SimpleDateFormat for date format to store date as string so that
* it is stored consistently.
*/
public static final SimpleDateFormat DATE_STORE_FORMAT
= new SimpleDateFormat("MM/dd/yyyy");
/**
* static SimpleDateFormat for time format to store time as string so that
* it is stored consistently.
*/
public static final SimpleDateFormat TIME_STORE_FORMAT
= new SimpleDateFormat("HH:mm:ss");
/**
* static SimpleDateFormat for datetime format to store date and time as
* string so that it is stored consistently.
*/
public static final SimpleDateFormat DATETIME_STORE_FORMAT
= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
/**
* static SimpleDateFormat for date format for sql date
*/
public static final SimpleDateFormat DATE_SQL_FORMAT
= new SimpleDateFormat("yyyy-MM-dd 00:00:00");
/**
* static SimpleDateFormat for time format for sql time
*/
public static final SimpleDateFormat TIME_SQL_FORMAT
= new SimpleDateFormat("1970-01-01 HH:mm:ss");
/**
* static SimpleDateFormat for datetime format for sql date and time
*/
public static final SimpleDateFormat DATETIME_SQL_FORMAT
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Constructors /////////////////////////////////////////////////////////////
/**
* Private constructor since this class cannot be instantiated
*/
private DateUtils(
)
{
// Do nothing
}
// Public methods ///////////////////////////////////////////////////////////
/**
* Check if two dates equals regardless of the time. Two null dates are equal.
* Null and not null dates are not equal.
*
* @param dtFirst - first date to compare, can be null
* @param dtSecond - second date to compare, can be null
* @return boolean - true if two dates equals regardless of what the time is
*/
public static boolean dateEquals(
Date dtFirst,
Date dtSecond
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
Calendar compCalendar;
int iEra;
int iYear;
int iMonth;
int iDay;
compCalendar = Calendar.getInstance();
compCalendar.setTime(dtFirst);
iEra = compCalendar.get(Calendar.ERA);
iYear = compCalendar.get(Calendar.YEAR);
iMonth = compCalendar.get(Calendar.MONTH);
iDay = compCalendar.get(Calendar.DATE);
compCalendar.setTime(dtSecond);
bReturn = ((iEra == compCalendar.get(Calendar.ERA))
&& (iYear == compCalendar.get(Calendar.YEAR))
&& (iMonth == compCalendar.get(Calendar.MONTH))
&& (iDay == compCalendar.get(Calendar.DATE)));
}
}
}
return bReturn;
}
/**
* Check if two times equals regardless of the date. Two null times are equal.
* Null and not null times are not equal.
*
* @param dtFirst - first time to compare, can be null
* @param dtSecond - second time to compare, can be null
* @param bIgnoreMilliseconds - if true milliseconds will be ignored in comparison
* @return boolean - true if two time equals regardless of what the date is
*/
public static boolean timeEquals(
Date dtFirst,
Date dtSecond,
boolean bIgnoreMilliseconds
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
Calendar compCalendar;
int iHour;
int iMinute;
int iSecond;
int iMili;
compCalendar = Calendar.getInstance();
compCalendar.setTime(dtFirst);
iHour = compCalendar.get(Calendar.HOUR_OF_DAY);
iMinute = compCalendar.get(Calendar.MINUTE);
iSecond = compCalendar.get(Calendar.SECOND);
iMili = compCalendar.get(Calendar.MILLISECOND);
compCalendar.setTime(dtSecond);
bReturn = ((iHour == compCalendar.get(Calendar.HOUR_OF_DAY))
&& (iMinute == compCalendar.get(Calendar.MINUTE))
&& (iSecond == compCalendar.get(Calendar.SECOND))
&& ((bIgnoreMilliseconds)
|| (iMili == compCalendar.get(Calendar.MILLISECOND))));
}
}
}
return bReturn;
}
/**
* Check if two dates and times are equal. Two null dates are equal. Null
* and not null dates are not equal.
*
* @param dtFirst - first date time to compare, can be null
* @param dtSecond - second date time to compare, can be null
* @return boolean - true if two date and times are equal
*/
public static boolean dateAndTimeEquals(
Date dtFirst,
Date dtSecond
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
// They are both not null so they have to match to millisecond
// (actually to nanosecond since the getTime takes nanoseconds
// into account)
bReturn = (dtFirst.getTime() == dtSecond.getTime());
}
}
}
return bReturn;
}
/**
* Check if String representing date is function or date. Date is a function
* (formula) if it starts with the current date/time variable which can be
* followed by expression describing period from current date.
*
* @param strValue - string representation of date or date function
* @return boolean - date function flag
*/
public static boolean isFunction(
String strValue
)
{
boolean bReturn = false;
if ((strValue != null) && (strValue.length() > 0)
&& (strValue.trim().startsWith(CURRENT_DATE_CODE)))
{
bReturn = true;
}
return bReturn;
}
/**
* Parse date time value from given string resolving any functions or formulas
* the string can contain. This method can be therefore used if the passed
* string contains string representation date, time or timestamp or a formula
* such as now + 3h - 1m + 4d.
*
* @param strValue - string representation of date or date function
* @param iDateType - date type code, one of the DATE_TYPE_XXX constants
* @param stored - flag if Date should be parsed using format used for
* storage or for display
* @return Timestamp - parsed date or null if date was null
* @throws OSSInvalidDataException - error during parsing
*/
public static Timestamp parseDateTime(
String strValue,
int iDateType,
boolean stored
)
{
Timestamp tsReturn = null;
Calendar workCal = GregorianCalendar.getInstance();
if (strValue != null && strValue.length() > 0)
{
strValue = strValue.trim();
if (strValue.startsWith(CURRENT_DATE_CODE))
{
strValue = strValue.replaceAll("[ ]", "");
// If the user specified "UseCurrent", then substitute the
// current date/time in the value
workCal.setTime(new Date());
// Log.getInstance().debug("Parsing current date " + strValue);
// Parse the date math
int iBeginIndex = CURRENT_DATE_CODE.length();
int iMaxLength = strValue.length();
int iSign = 1;
int iNumberIndex;
int iValue;
char cChar = " ";
while (iBeginIndex < iMaxLength)
{
// This has to be sign
if (strValue.charAt(iBeginIndex) == "+")
{
iSign = 1;
}
else if (strValue.charAt(iBeginIndex) == "-")
{
iSign = -1;
}
else
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iBeginIndex));
}
iBeginIndex++;
// Now we have to have number
iNumberIndex = iBeginIndex;
while (((iBeginIndex == iNumberIndex) || Character.isDigit(cChar))
&& (iBeginIndex < iMaxLength))
{
cChar = strValue.charAt(iBeginIndex++);
}
// We have to go one back because we should stop on modifier (e.g 1m)
iBeginIndex--;
try
{
iValue = Integer.parseInt(strValue.substring(iNumberIndex, iBeginIndex));
}
catch (NumberFormatException nmeExc)
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iNumberIndex));
}
// This has to be modifier: y - year, M - month, w - week,
// d - day, h - hour, m - minute, s - second
cChar = strValue.charAt(iBeginIndex);
switch(cChar)
{
case(YEAR_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used YEAR modifier for TIME type");
}
workCal.add(Calendar.YEAR, iSign * iValue);
break;
}
case(MONTH_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used MONTH modifier for TIME type");
}
workCal.add(Calendar.MONTH, iSign * iValue);
break;
}
case(WEEK_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used WEEK modifier for TIME type");
}
workCal.add(Calendar.WEEK_OF_YEAR, iSign * iValue);
break;
}
case(DAY_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used DAY modifier for TIME type");
}
workCal.add(Calendar.DATE, iSign * iValue);
break;
}
case(HOUR_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used HOUR modifier for DATE type");
}
workCal.add(Calendar.HOUR, iSign * iValue);
break;
}
case(MINUTE_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used MINUTE modifier for DATE type");
}
workCal.add(Calendar.MINUTE, iSign * iValue);
break;
}
case(SECOND_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used SECOND modifier for DATE type");
}
workCal.add(Calendar.SECOND, iSign * iValue);
break;
}
default:
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iBeginIndex));
}
}
iBeginIndex++;
}
tsReturn = new Timestamp(workCal.getTimeInMillis());
}
else
{
try
{
if (stored)
{
switch (iDateType)
{
case (DATE_TYPE_DATE) :
{
tsReturn = new Timestamp(DATE_STORE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_TIME) :
{
tsReturn = new Timestamp(TIME_STORE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_DATETIME) :
{
tsReturn = new Timestamp(DATETIME_STORE_FORMAT.parse(strValue).getTime());
break;
}
default:
{
assert false : "Unknown date type " + iDateType;
}
}
}
else
{
switch (iDateType)
{
case (DATE_TYPE_DATE) :
{
tsReturn = new Timestamp(DATE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_TIME) :
{
tsReturn = new Timestamp(TIME_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_DATETIME) :
{
tsReturn = new Timestamp(DATETIME_FORMAT.parse(strValue).getTime());
break;
}
default:
{
assert false : "Unknown date type " + iDateType;
}
}
}
}
catch (ParseException peExc)
{
throw new RuntimeException(
"Date is in incorrect format. Problems with parsing.",
peExc);
}
}
}
return tsReturn;
}
/**
* Parse the specified period into string displaying number of days the
* period represents.
*
* @param lPeriod - period in miliseconds
* @return String - period in format "x day(s)" or "" if not valid period
*/
public static String parseDayPeriod(
long lPeriod
)
{
StringBuffer sbReturn = new StringBuffer();
long lDays = 0L;
if (lPeriod > 0)
{
// we will count each started day as counted day
lPeriod = lPeriod + DateUtils.ONE_DAY - 1;
lDays = lPeriod / DateUtils.ONE_DAY;
sbReturn.append(lDays);
if (lDays == 1L)
{
sbReturn.append(" day");
}
else
{
sbReturn.append(" days");
}
}
else
{
sbReturn.append("0 days");
}
return sbReturn.toString();
}
/**
* Parse the specified period into string displaying date and time the
* period represents.
*
* @param lPeriod - preiod in miliseconds
* @return String - period in format "x day(s) y hour(s) z minute(s)"
* or "" if not valid period
*/
public static String parseDayTimePeriod(
long lPeriod
)
{
StringBuffer sbReturn = new StringBuffer();
long lHelp = 0L;
if (lPeriod > 0)
{
lPeriod = lPeriod + DateUtils.ONE_MINUTE - 1;
// we will count each started day as counted day
lHelp = lPeriod / DateUtils.ONE_DAY;
if (lHelp > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" d ");
}
else
{
sbReturn.append(" d ");
}
}
lPeriod = lPeriod % DateUtils.ONE_DAY;
lHelp = lPeriod / DateUtils.ONE_HOUR;
if (lHelp > 0 || sbReturn.length() > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" h ");
}
else
{
sbReturn.append(" h ");
}
}
lPeriod = lPeriod % DateUtils.ONE_HOUR;
lHelp = lPeriod / DateUtils.ONE_MINUTE;
if (lHelp > 0 || sbReturn.length() > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" min");
}
else
{
sbReturn.append(" min");
}
}
}
else
{
sbReturn.append("0 min");
}
return sbReturn.toString();
}
// TODO: For Miro: Remove this code once all the code which referred to these
// was fixed. These should be moved to a GUI related class.
// /**
// * Method for list of timing types.
// *
// * @return List - list of timing types
// */
// public static List getTimingTypes(
// )
// {
// List lstTimingTypes = new ArrayList();
//
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MINUTES),
// "Minute(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_HOURS), "Hour(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_DAYS), "Day(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_WEEKS), "Week(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MONTHS), "Month(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_YEARS), "Year(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NEVER), "Never"));
//
// return lstTimingTypes;
// }
// /**
// * Method for list of timing types with None option.
// *
// * @return List - list of timing types
// */
// public static List getTimingTypesWithNone(
// )
// {
// List lstTimingTypes = new ArrayList();
//
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NONE), "None"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MINUTES),
// "Minute(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_HOURS), "Hour(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_DAYS), "Day(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_WEEKS), "Week(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MONTHS), "Month(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_YEARS), "Year(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NEVER), "Never"));
//
// return lstTimingTypes;
// }
// /**
// * Method for getting string name of the timing type.
// *
// * @param iTimingType - timing type constant
// * @return String - string name of timing type
// */
// public static String getTimingTypeName(
// int iTimingType
// )
// {
// String outTimingTypeName = "Never";
// switch (iTimingType)
// {
// case (DateUtils.TIMING_NEVER):
// {
// outTimingTypeName = "Never";
// break;
// }
// case (DateUtils.TIMING_MINUTES):
// {
// outTimingTypeName = "Minute(s)";
// break;
// }
// case (DateUtils.TIMING_HOURS):
// {
// outTimingTypeName = "Hour(s)";
// break;
// }
// case (DateUtils.TIMING_DAYS):
// {
// outTimingTypeName = "Day(s)";
// break;
// }
// case (DateUtils.TIMING_WEEKS):
// {
// outTimingTypeName = "Week(s)";
// break;
// }
// case (DateUtils.TIMING_MONTHS):
// {
// outTimingTypeName = "Month(s)";
// break;
// }
// case (DateUtils.TIMING_YEARS):
// {
// outTimingTypeName = "Year(s)";
// break;
// }
// case (DateUtils.TIMING_NONE):
// {
// outTimingTypeName = "None";
// break;
// }
// }
//
// return outTimingTypeName;
// }
/**
* Get expiration timestamp from start date, period type and duration. For
* example if the start date is now, period type is hour and duration is 2
* then the result will be timestamp representing now + 2 hours.
*
* @param tsStartDate - start date of period counting
* @param iPeriodType - one of the period type constant TIMING_XXX
* @param iPeriodDuration - period duration, number of time units specified
* by period type
* @return Timestamp - date of period expiration or null if any problem
*/
public static Timestamp getPeriodExpiration(
Timestamp tsStartDate,
int iPeriodType,
int iPeriodDuration
)
{
Timestamp tsReturn = null;
Calendar calHelp;
if (tsStartDate != null && iPeriodDuration > 0
&& iPeriodType > TIMING_NEVER && iPeriodType < TIMING_NONE)
{
calHelp = Calendar.getInstance();
calHelp.setTime(tsStartDate);
switch (iPeriodType)
{
case (TIMING_MINUTES) :
{
calHelp.add(Calendar.MINUTE, iPeriodDuration);
break;
}
case (TIMING_HOURS) :
{
calHelp.add(Calendar.HOUR, iPeriodDuration);
break;
}
case (TIMING_DAYS) :
{
calHelp.add(Calendar.DATE, iPeriodDuration);
break;
}
case (TIMING_WEEKS) :
{
calHelp.add(Calendar.WEEK_OF_YEAR, iPeriodDuration);
break;
}
case (TIMING_MONTHS) :
{
calHelp.add(Calendar.MONTH, iPeriodDuration);
break;
}
case (TIMING_YEARS) :
{
calHelp.add(Calendar.YEAR, iPeriodDuration);
break;
}
default :
{
assert false : "Not supported Timing type " + iPeriodType;
}
}
tsReturn = new Timestamp(calHelp.getTimeInMillis());
}
return tsReturn;
}
/**
* Method to compare time periods
*
* @param iPeriodType1 - first period type, one of the period type constant
* TIMING_XXX
* @param iPeriodDuration1 - first period duration
* @param iPeriodType2 - second period type, one of the period type constant
* TIMING_XXX
* @param iPeriodDuration2 - second period duration
* @return int - 1 - first period is longer
* 0 - periods are same
* -1 - first period is shorter
*/
public static int comparePeriods(
int iPeriodType1,
int iPeriodDuration1,
int iPeriodType2,
int iPeriodDuration2
)
{
int iReturn = 0;
if ((iPeriodType1 != TIMING_NEVER) && (iPeriodType1 != TIMING_NONE)
&& (iPeriodType2 != TIMING_NEVER) && (iPeriodType2 != TIMING_NONE))
{
Timestamp tsTimestamp1 = getPeriodExpiration(
new Timestamp(0), iPeriodType1, iPeriodDuration1);
Timestamp tsTimestamp2 = getPeriodExpiration(
new Timestamp(0), iPeriodType2, iPeriodDuration2);
// TODO: Improve: When would any of these be null?
if ((tsTimestamp1 != null) && (tsTimestamp2 != null))
{
if (tsTimestamp1.after(tsTimestamp2))
{
iReturn = 1;
}
else if (tsTimestamp2.after(tsTimestamp1))
{
iReturn = -1;
}
}
}
else
{
if (iPeriodType1 != iPeriodType2)
{
if (iPeriodType1 == TIMING_NEVER)
{
iReturn = 1;
}
else if (iPeriodType1 == TIMING_NONE)
{
iReturn = -1;
}
else if (iPeriodType2 == TIMING_NEVER)
{
iReturn = -1;
}
else if (iPeriodType2 == TIMING_NONE)
{
iReturn = 1;
}
}
}
return iReturn;
}
/**
* Convert timestamp to string including it"s nanosecond portion so that it
* can be safely stored in variable of web page.
*
* @param tsTimestamp - timestamp to convert
* @return String - text containing time and nanosecond portion of timestamp
*/
public static String getTimestampAsString(
Timestamp tsTimestamp
)
{
StringBuffer sbTimestamp = new StringBuffer();
sbTimestamp.append(tsTimestamp.getTime());
sbTimestamp.append(NANO_SEPARATOR);
sbTimestamp.append(tsTimestamp.getNanos());
return sbTimestamp.toString();
}
/**
* Function returns time string in the form MM:SS.MS from the input specified in miliseconds.
*
* @param lTimeInMiliseconds - time in miliseconds
* @return String - string representation of miliseconds in the form MM:SS.MS
*/
public static String getStringTime(
long lTimeInMiliseconds
)
{
long lTotalMS = lTimeInMiliseconds;
long lMS = lTotalMS % 1000;
long lTotalSecs = lTotalMS / 1000;
long lSecs = lTotalSecs % 60;
long lTotalMins = lTotalSecs / 60;
long lMinutes = lTotalMins % 60;
long lHours = lTotalMins / 60;
StringBuffer sbBuffer = new StringBuffer();
if (lHours > 0)
{
sbBuffer.append(lHours);
sbBuffer.append(":");
sbBuffer.append(lMinutes);
sbBuffer.append(":");
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
}
else if (lMinutes > 0)
{
sbBuffer.append(lMinutes);
sbBuffer.append(":");
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
}
else if (lSecs > 0)
{
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
sbBuffer.append(" seconds");
}
else
{
sbBuffer.append(lMS);
sbBuffer.append(" ms");
}
return sbBuffer.toString();
}
// TODO: For Miro: Remove this code once all the code which referred to these
// was fixed. These should be moved to a GUI or business logic related class.
// /**
// * Method to check if valid period settings
// *
// * @param iPeriod - period length
// * @param iPeriodType - period type
// * @param iPeriodStartType - period start type
// * @param iAttributeId - attribute ID for dynamic period start type
// * @param bPeriodException - period exception flag
// * @param strPeriodName - period name used for exception message
// * @param bAdvancePeriodType - flag if advanced period type (includes also start type)
// * @param bfideException - invalid data exception
// */
// public static void validatePeriod(
// int iPeriod,
// int iPeriodType,
// int iPeriodStartType,
// int iAttributeId,
// boolean bPeriodException,
// String strPeriodName,
// boolean bAdvancePeriodType,
// OSSInvalidDataException messageException
// )
// {
// if ((iPeriod > 0)
// || ((iPeriodType != TIMING_NONE) && (iPeriodType != TIMING_NEVER))
// || (bPeriodException) || (iPeriodStartType != PERIOD_START_TYPE_NONE))
// {
// if (iPeriod <= 0)
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period length for " + strPeriodName + " type."
// );
// }
// else if ((iPeriodType == TIMING_NONE) || (iPeriodType == TIMING_NEVER))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period type for " + strPeriodName + " type."
// );
// }
// else if ((bAdvancePeriodType) && (iPeriodStartType == PERIOD_START_TYPE_NONE))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period start type for " + strPeriodName + " type."
// );
// }
// else if ((bAdvancePeriodType)
// && (iPeriodStartType == PERIOD_START_TYPE_DYNAMIC)
// && (iAttributeId == DataObject.NEW_ID))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period dynamic start attribute for "
// + strPeriodName + " type."
// );
// }
// }
// }
}
Timestamp parse
/**
* Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
*
* Project: OpenSubsystems
*
* $Id: DateUtils.java,v 1.7 2007/01/07 06:14:00 bastafidli Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Collection of useful utilities to work with dates.
*
* @version $Id: DateUtils.java,v 1.7 2007/01/07 06:14:00 bastafidli Exp $
* @author Miro Halas
* @code.reviewer Miro Halas
* @code.reviewed 1.5 2005/09/13 13:23:15 bastafidli
*/
public final class DateUtils
{
// Constants ////////////////////////////////////////////////////////////////
/**
* One second in milliseconds.
*/
public static final long ONE_SECOND = 1000L;
/**
* One minute in milliseconds.
*/
public static final long ONE_MINUTE = ONE_SECOND * 60L;
/**
* One hour in milliseconds.
*/
public static final long ONE_HOUR = ONE_MINUTE * 60L;
/**
* One day in milliseconds.
*/
public static final long ONE_DAY = ONE_HOUR * 24L;
/**
* Separator we used to separate time from the nanosecond portion of the
* timestamp when converted to string.
*/
public static final char NANO_SEPARATOR = ":";
/**
* Constant for timing type
*/
public static final int TIMING_NEVER = 0;
/**
* Constant for timing type
*/
public static final int TIMING_MINUTES = 1;
/**
* Constant for timing type
*/
public static final int TIMING_HOURS = 2;
/**
* Constant for timing type
*/
public static final int TIMING_DAYS = 3;
/**
* Constant for timing type
*/
public static final int TIMING_WEEKS = 4;
/**
* Constant for timing type
*/
public static final int TIMING_MONTHS = 5;
/**
* Constant for timing type
*/
public static final int TIMING_YEARS = 6;
/**
* Constant for timing type
*/
public static final int TIMING_NONE = 7;
/**
* Constant for current date code used in date/time formulas
*/
public static final String CURRENT_DATE_CODE = "now";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char YEAR_CODE = "y";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char MONTH_CODE = "M";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char WEEK_CODE = "w";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char DAY_CODE = "d";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char HOUR_CODE = "h";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char MINUTE_CODE = "m";
/**
* Constant for dynamic date code used in date/time formulas
*/
public static final char SECOND_CODE = "s";
/**
* constant for date type DATE
*/
public static final int DATE_TYPE_DATE = 1;
/**
* constant for date type TIME
*/
public static final int DATE_TYPE_TIME = 2;
/**
* constant for date type DATETIME
*/
public static final int DATE_TYPE_DATETIME = 3;
// Constants for period start types /////////////////////////////////////////
// TODO: For Miro: Remove this code once all the code which referred to these
// constants was fixed
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_NONE = 0;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_CREATION = 1;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_COMPLETION = 2;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_APPROVAL = 3;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_ACTIVATION = 4;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_INACTIVATION = 5;
//
// /**
// * constant for period type
// */
// public static final int PERIOD_START_TYPE_DYNAMIC = 6;
//
// /**
// * constant for period type code
// */
// public static final int PERIOD_TYPE_CODE = 99;
//
// /**
// * constant for period type object
// */
// public static final Integer PERIOD_TYPE_OBJ = new Integer(PERIOD_TYPE_CODE);
// Cached variables /////////////////////////////////////////////////////////
/**
* static SimpleDateFormat for date format to display on UI and in messages.
*/
public static final SimpleDateFormat DATE_FORMAT
= (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT);
/**
* static SimpleDateFormat for time format to display on UI and in messages.
*/
public static final SimpleDateFormat TIME_FORMAT
= (SimpleDateFormat) DateFormat.getTimeInstance(DateFormat.MEDIUM);
/**
* static SimpleDateFormat for datetime format to display on UI and in messages.
*/
public static final SimpleDateFormat DATETIME_FORMAT
= (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,
DateFormat.MEDIUM);
/**
* static SimpleDateFormat for date format to store date as string so that
* it is stored consistently.
*/
public static final SimpleDateFormat DATE_STORE_FORMAT
= new SimpleDateFormat("MM/dd/yyyy");
/**
* static SimpleDateFormat for time format to store time as string so that
* it is stored consistently.
*/
public static final SimpleDateFormat TIME_STORE_FORMAT
= new SimpleDateFormat("HH:mm:ss");
/**
* static SimpleDateFormat for datetime format to store date and time as
* string so that it is stored consistently.
*/
public static final SimpleDateFormat DATETIME_STORE_FORMAT
= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
/**
* static SimpleDateFormat for date format for sql date
*/
public static final SimpleDateFormat DATE_SQL_FORMAT
= new SimpleDateFormat("yyyy-MM-dd 00:00:00");
/**
* static SimpleDateFormat for time format for sql time
*/
public static final SimpleDateFormat TIME_SQL_FORMAT
= new SimpleDateFormat("1970-01-01 HH:mm:ss");
/**
* static SimpleDateFormat for datetime format for sql date and time
*/
public static final SimpleDateFormat DATETIME_SQL_FORMAT
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Constructors /////////////////////////////////////////////////////////////
/**
* Private constructor since this class cannot be instantiated
*/
private DateUtils(
)
{
// Do nothing
}
// Public methods ///////////////////////////////////////////////////////////
/**
* Check if two dates equals regardless of the time. Two null dates are equal.
* Null and not null dates are not equal.
*
* @param dtFirst - first date to compare, can be null
* @param dtSecond - second date to compare, can be null
* @return boolean - true if two dates equals regardless of what the time is
*/
public static boolean dateEquals(
Date dtFirst,
Date dtSecond
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
Calendar compCalendar;
int iEra;
int iYear;
int iMonth;
int iDay;
compCalendar = Calendar.getInstance();
compCalendar.setTime(dtFirst);
iEra = compCalendar.get(Calendar.ERA);
iYear = compCalendar.get(Calendar.YEAR);
iMonth = compCalendar.get(Calendar.MONTH);
iDay = compCalendar.get(Calendar.DATE);
compCalendar.setTime(dtSecond);
bReturn = ((iEra == compCalendar.get(Calendar.ERA))
&& (iYear == compCalendar.get(Calendar.YEAR))
&& (iMonth == compCalendar.get(Calendar.MONTH))
&& (iDay == compCalendar.get(Calendar.DATE)));
}
}
}
return bReturn;
}
/**
* Check if two times equals regardless of the date. Two null times are equal.
* Null and not null times are not equal.
*
* @param dtFirst - first time to compare, can be null
* @param dtSecond - second time to compare, can be null
* @param bIgnoreMilliseconds - if true milliseconds will be ignored in comparison
* @return boolean - true if two time equals regardless of what the date is
*/
public static boolean timeEquals(
Date dtFirst,
Date dtSecond,
boolean bIgnoreMilliseconds
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
Calendar compCalendar;
int iHour;
int iMinute;
int iSecond;
int iMili;
compCalendar = Calendar.getInstance();
compCalendar.setTime(dtFirst);
iHour = compCalendar.get(Calendar.HOUR_OF_DAY);
iMinute = compCalendar.get(Calendar.MINUTE);
iSecond = compCalendar.get(Calendar.SECOND);
iMili = compCalendar.get(Calendar.MILLISECOND);
compCalendar.setTime(dtSecond);
bReturn = ((iHour == compCalendar.get(Calendar.HOUR_OF_DAY))
&& (iMinute == compCalendar.get(Calendar.MINUTE))
&& (iSecond == compCalendar.get(Calendar.SECOND))
&& ((bIgnoreMilliseconds)
|| (iMili == compCalendar.get(Calendar.MILLISECOND))));
}
}
}
return bReturn;
}
/**
* Check if two dates and times are equal. Two null dates are equal. Null
* and not null dates are not equal.
*
* @param dtFirst - first date time to compare, can be null
* @param dtSecond - second date time to compare, can be null
* @return boolean - true if two date and times are equal
*/
public static boolean dateAndTimeEquals(
Date dtFirst,
Date dtSecond
)
{
boolean bReturn = false;
// If they are the same object, they are equals
bReturn = (dtFirst == dtSecond);
if (!bReturn)
{
if (dtFirst == null)
{
// Two null dates are the same
bReturn = (dtSecond == null);
}
else
{
if (dtSecond != null)
{
// They are both not null so they have to match to millisecond
// (actually to nanosecond since the getTime takes nanoseconds
// into account)
bReturn = (dtFirst.getTime() == dtSecond.getTime());
}
}
}
return bReturn;
}
/**
* Check if String representing date is function or date. Date is a function
* (formula) if it starts with the current date/time variable which can be
* followed by expression describing period from current date.
*
* @param strValue - string representation of date or date function
* @return boolean - date function flag
*/
public static boolean isFunction(
String strValue
)
{
boolean bReturn = false;
if ((strValue != null) && (strValue.length() > 0)
&& (strValue.trim().startsWith(CURRENT_DATE_CODE)))
{
bReturn = true;
}
return bReturn;
}
/**
* Parse date time value from given string resolving any functions or formulas
* the string can contain. This method can be therefore used if the passed
* string contains string representation date, time or timestamp or a formula
* such as now + 3h - 1m + 4d.
*
* @param strValue - string representation of date or date function
* @param iDateType - date type code, one of the DATE_TYPE_XXX constants
* @param stored - flag if Date should be parsed using format used for
* storage or for display
* @return Timestamp - parsed date or null if date was null
* @throws OSSInvalidDataException - error during parsing
*/
public static Timestamp parseDateTime(
String strValue,
int iDateType,
boolean stored
)
{
Timestamp tsReturn = null;
Calendar workCal = GregorianCalendar.getInstance();
if (strValue != null && strValue.length() > 0)
{
strValue = strValue.trim();
if (strValue.startsWith(CURRENT_DATE_CODE))
{
strValue = strValue.replaceAll("[ ]", "");
// If the user specified "UseCurrent", then substitute the
// current date/time in the value
workCal.setTime(new Date());
// Log.getInstance().debug("Parsing current date " + strValue);
// Parse the date math
int iBeginIndex = CURRENT_DATE_CODE.length();
int iMaxLength = strValue.length();
int iSign = 1;
int iNumberIndex;
int iValue;
char cChar = " ";
while (iBeginIndex < iMaxLength)
{
// This has to be sign
if (strValue.charAt(iBeginIndex) == "+")
{
iSign = 1;
}
else if (strValue.charAt(iBeginIndex) == "-")
{
iSign = -1;
}
else
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iBeginIndex));
}
iBeginIndex++;
// Now we have to have number
iNumberIndex = iBeginIndex;
while (((iBeginIndex == iNumberIndex) || Character.isDigit(cChar))
&& (iBeginIndex < iMaxLength))
{
cChar = strValue.charAt(iBeginIndex++);
}
// We have to go one back because we should stop on modifier (e.g 1m)
iBeginIndex--;
try
{
iValue = Integer.parseInt(strValue.substring(iNumberIndex, iBeginIndex));
}
catch (NumberFormatException nmeExc)
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iNumberIndex));
}
// This has to be modifier: y - year, M - month, w - week,
// d - day, h - hour, m - minute, s - second
cChar = strValue.charAt(iBeginIndex);
switch(cChar)
{
case(YEAR_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used YEAR modifier for TIME type");
}
workCal.add(Calendar.YEAR, iSign * iValue);
break;
}
case(MONTH_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used MONTH modifier for TIME type");
}
workCal.add(Calendar.MONTH, iSign * iValue);
break;
}
case(WEEK_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used WEEK modifier for TIME type");
}
workCal.add(Calendar.WEEK_OF_YEAR, iSign * iValue);
break;
}
case(DAY_CODE):
{
if (iDateType == DATE_TYPE_TIME)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used DAY modifier for TIME type");
}
workCal.add(Calendar.DATE, iSign * iValue);
break;
}
case(HOUR_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used HOUR modifier for DATE type");
}
workCal.add(Calendar.HOUR, iSign * iValue);
break;
}
case(MINUTE_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used MINUTE modifier for DATE type");
}
workCal.add(Calendar.MINUTE, iSign * iValue);
break;
}
case(SECOND_CODE):
{
if (iDateType == DATE_TYPE_DATE)
{
throw new RuntimeException(
"Date function is in incorrect format: " +
"used SECOND modifier for DATE type");
}
workCal.add(Calendar.SECOND, iSign * iValue);
break;
}
default:
{
// Incorrect String
throw new RuntimeException(
"Date function is in incorrect format: "
+ strValue + " at " + strValue.substring(iBeginIndex));
}
}
iBeginIndex++;
}
tsReturn = new Timestamp(workCal.getTimeInMillis());
}
else
{
try
{
if (stored)
{
switch (iDateType)
{
case (DATE_TYPE_DATE) :
{
tsReturn = new Timestamp(DATE_STORE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_TIME) :
{
tsReturn = new Timestamp(TIME_STORE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_DATETIME) :
{
tsReturn = new Timestamp(DATETIME_STORE_FORMAT.parse(strValue).getTime());
break;
}
default:
{
assert false : "Unknown date type " + iDateType;
}
}
}
else
{
switch (iDateType)
{
case (DATE_TYPE_DATE) :
{
tsReturn = new Timestamp(DATE_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_TIME) :
{
tsReturn = new Timestamp(TIME_FORMAT.parse(strValue).getTime());
break;
}
case (DATE_TYPE_DATETIME) :
{
tsReturn = new Timestamp(DATETIME_FORMAT.parse(strValue).getTime());
break;
}
default:
{
assert false : "Unknown date type " + iDateType;
}
}
}
}
catch (ParseException peExc)
{
throw new RuntimeException(
"Date is in incorrect format. Problems with parsing.",
peExc);
}
}
}
return tsReturn;
}
/**
* Parse the specified period into string displaying number of days the
* period represents.
*
* @param lPeriod - period in miliseconds
* @return String - period in format "x day(s)" or "" if not valid period
*/
public static String parseDayPeriod(
long lPeriod
)
{
StringBuffer sbReturn = new StringBuffer();
long lDays = 0L;
if (lPeriod > 0)
{
// we will count each started day as counted day
lPeriod = lPeriod + DateUtils.ONE_DAY - 1;
lDays = lPeriod / DateUtils.ONE_DAY;
sbReturn.append(lDays);
if (lDays == 1L)
{
sbReturn.append(" day");
}
else
{
sbReturn.append(" days");
}
}
else
{
sbReturn.append("0 days");
}
return sbReturn.toString();
}
/**
* Parse the specified period into string displaying date and time the
* period represents.
*
* @param lPeriod - preiod in miliseconds
* @return String - period in format "x day(s) y hour(s) z minute(s)"
* or "" if not valid period
*/
public static String parseDayTimePeriod(
long lPeriod
)
{
StringBuffer sbReturn = new StringBuffer();
long lHelp = 0L;
if (lPeriod > 0)
{
lPeriod = lPeriod + DateUtils.ONE_MINUTE - 1;
// we will count each started day as counted day
lHelp = lPeriod / DateUtils.ONE_DAY;
if (lHelp > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" d ");
}
else
{
sbReturn.append(" d ");
}
}
lPeriod = lPeriod % DateUtils.ONE_DAY;
lHelp = lPeriod / DateUtils.ONE_HOUR;
if (lHelp > 0 || sbReturn.length() > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" h ");
}
else
{
sbReturn.append(" h ");
}
}
lPeriod = lPeriod % DateUtils.ONE_HOUR;
lHelp = lPeriod / DateUtils.ONE_MINUTE;
if (lHelp > 0 || sbReturn.length() > 0)
{
sbReturn.append(lHelp);
if (lHelp == 1L)
{
sbReturn.append(" min");
}
else
{
sbReturn.append(" min");
}
}
}
else
{
sbReturn.append("0 min");
}
return sbReturn.toString();
}
// TODO: For Miro: Remove this code once all the code which referred to these
// was fixed. These should be moved to a GUI related class.
// /**
// * Method for list of timing types.
// *
// * @return List - list of timing types
// */
// public static List getTimingTypes(
// )
// {
// List lstTimingTypes = new ArrayList();
//
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MINUTES),
// "Minute(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_HOURS), "Hour(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_DAYS), "Day(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_WEEKS), "Week(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MONTHS), "Month(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_YEARS), "Year(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NEVER), "Never"));
//
// return lstTimingTypes;
// }
// /**
// * Method for list of timing types with None option.
// *
// * @return List - list of timing types
// */
// public static List getTimingTypesWithNone(
// )
// {
// List lstTimingTypes = new ArrayList();
//
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NONE), "None"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MINUTES),
// "Minute(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_HOURS), "Hour(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_DAYS), "Day(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_WEEKS), "Week(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_MONTHS), "Month(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_YEARS), "Year(s)"));
// lstTimingTypes.add(new SelectOption(Integer.toString(DateUtils.TIMING_NEVER), "Never"));
//
// return lstTimingTypes;
// }
// /**
// * Method for getting string name of the timing type.
// *
// * @param iTimingType - timing type constant
// * @return String - string name of timing type
// */
// public static String getTimingTypeName(
// int iTimingType
// )
// {
// String outTimingTypeName = "Never";
// switch (iTimingType)
// {
// case (DateUtils.TIMING_NEVER):
// {
// outTimingTypeName = "Never";
// break;
// }
// case (DateUtils.TIMING_MINUTES):
// {
// outTimingTypeName = "Minute(s)";
// break;
// }
// case (DateUtils.TIMING_HOURS):
// {
// outTimingTypeName = "Hour(s)";
// break;
// }
// case (DateUtils.TIMING_DAYS):
// {
// outTimingTypeName = "Day(s)";
// break;
// }
// case (DateUtils.TIMING_WEEKS):
// {
// outTimingTypeName = "Week(s)";
// break;
// }
// case (DateUtils.TIMING_MONTHS):
// {
// outTimingTypeName = "Month(s)";
// break;
// }
// case (DateUtils.TIMING_YEARS):
// {
// outTimingTypeName = "Year(s)";
// break;
// }
// case (DateUtils.TIMING_NONE):
// {
// outTimingTypeName = "None";
// break;
// }
// }
//
// return outTimingTypeName;
// }
/**
* Get expiration timestamp from start date, period type and duration. For
* example if the start date is now, period type is hour and duration is 2
* then the result will be timestamp representing now + 2 hours.
*
* @param tsStartDate - start date of period counting
* @param iPeriodType - one of the period type constant TIMING_XXX
* @param iPeriodDuration - period duration, number of time units specified
* by period type
* @return Timestamp - date of period expiration or null if any problem
*/
public static Timestamp getPeriodExpiration(
Timestamp tsStartDate,
int iPeriodType,
int iPeriodDuration
)
{
Timestamp tsReturn = null;
Calendar calHelp;
if (tsStartDate != null && iPeriodDuration > 0
&& iPeriodType > TIMING_NEVER && iPeriodType < TIMING_NONE)
{
calHelp = Calendar.getInstance();
calHelp.setTime(tsStartDate);
switch (iPeriodType)
{
case (TIMING_MINUTES) :
{
calHelp.add(Calendar.MINUTE, iPeriodDuration);
break;
}
case (TIMING_HOURS) :
{
calHelp.add(Calendar.HOUR, iPeriodDuration);
break;
}
case (TIMING_DAYS) :
{
calHelp.add(Calendar.DATE, iPeriodDuration);
break;
}
case (TIMING_WEEKS) :
{
calHelp.add(Calendar.WEEK_OF_YEAR, iPeriodDuration);
break;
}
case (TIMING_MONTHS) :
{
calHelp.add(Calendar.MONTH, iPeriodDuration);
break;
}
case (TIMING_YEARS) :
{
calHelp.add(Calendar.YEAR, iPeriodDuration);
break;
}
default :
{
assert false : "Not supported Timing type " + iPeriodType;
}
}
tsReturn = new Timestamp(calHelp.getTimeInMillis());
}
return tsReturn;
}
/**
* Method to compare time periods
*
* @param iPeriodType1 - first period type, one of the period type constant
* TIMING_XXX
* @param iPeriodDuration1 - first period duration
* @param iPeriodType2 - second period type, one of the period type constant
* TIMING_XXX
* @param iPeriodDuration2 - second period duration
* @return int - 1 - first period is longer
* 0 - periods are same
* -1 - first period is shorter
*/
public static int comparePeriods(
int iPeriodType1,
int iPeriodDuration1,
int iPeriodType2,
int iPeriodDuration2
)
{
int iReturn = 0;
if ((iPeriodType1 != TIMING_NEVER) && (iPeriodType1 != TIMING_NONE)
&& (iPeriodType2 != TIMING_NEVER) && (iPeriodType2 != TIMING_NONE))
{
Timestamp tsTimestamp1 = getPeriodExpiration(
new Timestamp(0), iPeriodType1, iPeriodDuration1);
Timestamp tsTimestamp2 = getPeriodExpiration(
new Timestamp(0), iPeriodType2, iPeriodDuration2);
// TODO: Improve: When would any of these be null?
if ((tsTimestamp1 != null) && (tsTimestamp2 != null))
{
if (tsTimestamp1.after(tsTimestamp2))
{
iReturn = 1;
}
else if (tsTimestamp2.after(tsTimestamp1))
{
iReturn = -1;
}
}
}
else
{
if (iPeriodType1 != iPeriodType2)
{
if (iPeriodType1 == TIMING_NEVER)
{
iReturn = 1;
}
else if (iPeriodType1 == TIMING_NONE)
{
iReturn = -1;
}
else if (iPeriodType2 == TIMING_NEVER)
{
iReturn = -1;
}
else if (iPeriodType2 == TIMING_NONE)
{
iReturn = 1;
}
}
}
return iReturn;
}
/**
* Convert timestamp to string including it"s nanosecond portion so that it
* can be safely stored in variable of web page.
*
* @param tsTimestamp - timestamp to convert
* @return String - text containing time and nanosecond portion of timestamp
*/
public static String getTimestampAsString(
Timestamp tsTimestamp
)
{
StringBuffer sbTimestamp = new StringBuffer();
sbTimestamp.append(tsTimestamp.getTime());
sbTimestamp.append(NANO_SEPARATOR);
sbTimestamp.append(tsTimestamp.getNanos());
return sbTimestamp.toString();
}
/**
* Function returns time string in the form MM:SS.MS from the input specified in miliseconds.
*
* @param lTimeInMiliseconds - time in miliseconds
* @return String - string representation of miliseconds in the form MM:SS.MS
*/
public static String getStringTime(
long lTimeInMiliseconds
)
{
long lTotalMS = lTimeInMiliseconds;
long lMS = lTotalMS % 1000;
long lTotalSecs = lTotalMS / 1000;
long lSecs = lTotalSecs % 60;
long lTotalMins = lTotalSecs / 60;
long lMinutes = lTotalMins % 60;
long lHours = lTotalMins / 60;
StringBuffer sbBuffer = new StringBuffer();
if (lHours > 0)
{
sbBuffer.append(lHours);
sbBuffer.append(":");
sbBuffer.append(lMinutes);
sbBuffer.append(":");
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
}
else if (lMinutes > 0)
{
sbBuffer.append(lMinutes);
sbBuffer.append(":");
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
}
else if (lSecs > 0)
{
sbBuffer.append(lSecs);
sbBuffer.append(".");
sbBuffer.append(lMS);
sbBuffer.append(" seconds");
}
else
{
sbBuffer.append(lMS);
sbBuffer.append(" ms");
}
return sbBuffer.toString();
}
// TODO: For Miro: Remove this code once all the code which referred to these
// was fixed. These should be moved to a GUI or business logic related class.
// /**
// * Method to check if valid period settings
// *
// * @param iPeriod - period length
// * @param iPeriodType - period type
// * @param iPeriodStartType - period start type
// * @param iAttributeId - attribute ID for dynamic period start type
// * @param bPeriodException - period exception flag
// * @param strPeriodName - period name used for exception message
// * @param bAdvancePeriodType - flag if advanced period type (includes also start type)
// * @param bfideException - invalid data exception
// */
// public static void validatePeriod(
// int iPeriod,
// int iPeriodType,
// int iPeriodStartType,
// int iAttributeId,
// boolean bPeriodException,
// String strPeriodName,
// boolean bAdvancePeriodType,
// OSSInvalidDataException messageException
// )
// {
// if ((iPeriod > 0)
// || ((iPeriodType != TIMING_NONE) && (iPeriodType != TIMING_NEVER))
// || (bPeriodException) || (iPeriodStartType != PERIOD_START_TYPE_NONE))
// {
// if (iPeriod <= 0)
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period length for " + strPeriodName + " type."
// );
// }
// else if ((iPeriodType == TIMING_NONE) || (iPeriodType == TIMING_NEVER))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period type for " + strPeriodName + " type."
// );
// }
// else if ((bAdvancePeriodType) && (iPeriodStartType == PERIOD_START_TYPE_NONE))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period start type for " + strPeriodName + " type."
// );
// }
// else if ((bAdvancePeriodType)
// && (iPeriodStartType == PERIOD_START_TYPE_DYNAMIC)
// && (iAttributeId == DataObject.NEW_ID))
// {
// if (messageException == null)
// {
// messageException = new OSSInvalidDataException();
// }
// messageException.getErrorMessages().addMessage(
// PERIOD_TYPE_OBJ,
// "You have to set valid period dynamic start attribute for "
// + strPeriodName + " type."
// );
// }
// }
// }
}