📄 dateutil.java
字号:
{
return MTH_AUG;
} else if("09".equals(mthMM))
{
return MTH_SEP;
} else if("10".equals(mthMM))
{
return MTH_OCT;
} else if("11".equals(mthMM))
{
return MTH_NOV;
} else if("12".equals(mthMM))
{
return MTH_DEC;
}
return null;
}
/**
* Converts the specified date-time string to SQL Date object based on the
* specified date-time format. <CODE>null</CODE> is returned if the
* specified date is invalid.
*
* @param strDateTime The date string in this format 'ddMMyyyy HHmm'.
* @param dateTimeFormat The date pattern in this format 'ddMMyyyy HHmm'
* @return the SQL Date representation. Returns null if the date object or
* the strDateTime or the dateTimeFormat is null.
*/
public static java.sql.Date toSQLDate(String strDateTime,
String dateTimeFormat)
{
Date date = toDate(strDateTime, dateTimeFormat);
if(date == null)
{
return null;
}
return new java.sql.Date(date.getTime());
}
/**
* Converts the Date object to SQL Date object.
*
* @param date The date to be converted.
* @return the SQL Date representation.
*/
public static java.sql.Date toSQLDate(Date date)
{
if(date == null)
{
return null;
}
return new java.sql.Date(date.getTime());
}
/**
* Converts the specified date-time string to SQL Date object based on the
* specified date-time format. <CODE>null</CODE> is returned if the
* specified date is invalid.
*
* @param strDateTime The date string in this format 'ddMMyyyy HHmm'.
* @return the SQL Date representation. Returns null if the date object or
* the strDateTime or the dateTimeFormat is null.
*/
public static java.sql.Date toSQLDate(String strDateTime)
{
return toSQLDate(strDateTime, DATETIME_FORMAT);
}
/**
* Converts the specified date-time string to Timestamp.
*
* @param dateTimeStr The String object in this ddMMyyyy HHmm format
* @return Timestamp object. Returns null if dateTimeStr is null Format
* used is meant for Oracle dbs only
*/
public static Timestamp toTimestamp(String dateTimeStr)
{
return toTimestamp(toDate(dateTimeStr));
}
/**
* Converts the specified date-time string to Timestamp.
*
* @param dateTimeStr The String object in this ddMMyyyy HHmm format
* @param dateTimeFormat The date pattern in this format 'ddMMyyyy HHmm'
* @return Timestamp object. Returns null if dateTimeStr is null Format
* used is meant for Oracle dbs only
*/
public static Timestamp toTimestamp(String dateTimeStr,
String dateTimeFormat)
{
return toTimestamp(toDate(dateTimeStr, dateTimeFormat));
}
/**
* Converts the specified Calendar to Timestamp.
*
* @param date The Date object.
* @return Timestamp object. Returns null if date object is null Format
* used is meant for Oracle dbs only
*/
public static Timestamp toTimestamp(Date date)
{
if(date == null)
{
return null;
}
return new Timestamp(date.getTime());
}
/**
* Converts the specified Calendar to Timestamp.
*
* @param timeStamp The TimeStamp object.
* @return Date object. Returns null if timestamp object is null Format
* used is meant for Oracle dbs only
*/
public static Date toDate(Timestamp timeStamp)
{
if(timeStamp == null)
{
return null;
}
return new Date(timeStamp.getTime());
}
/**
* to determine is the date is infinite. ie is the year is 99999.
* 9999 is used to detnote the expiry date does not expire for ever
*
* @param dateToCheck Date the date to check is it infinite
* @return result Boolean if the date is infinity it will return true. or it will return false
*/
public static Boolean isInfinity(Date dateToCheck)
{
if(dateToCheck == null)
{
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(dateToCheck);
int year = cal.get(Calendar.YEAR);
log.info("The date is infinity. The given year :" + year);
if(year >= 9999)
{
log.info("The date is infinity");
return Boolean.TRUE;
}
log.info("The date is not infinity");
return Boolean.FALSE;
}
/**
* this method is designed to calculate the first/last day of current week
*
* @return a Map object that contains:
* <pre>
* key value<br>
* startOfWeek the first day of current week<br>
* endOfWeek the last day of current week<br>
* </pre>
*/
public static Map calcCurrentWeek()
{
Calendar curCal = Calendar.getInstance();
int i = curCal.get(Calendar.DAY_OF_WEEK);
Date startOfWeek = addDaysToDate(curCal.getTime(), -i + 2);
Date endOfWeek = addDaysToDate(curCal.getTime(), 7 - i + 1);
Map resMap = new HashMap();
resMap.put("startOfWeek", startOfWeek);
resMap.put("endOfWeek", endOfWeek);
return resMap;
}
//~ Static fields/initializers =============================================
private static String timePattern = "HH:mm";
//~ Methods ================================================================
/**
* Return default datePattern (MM/dd/yyyy)
*
* @return a string representing the date pattern on the UI
*/
public static synchronized String getDatePattern()
{
// Locale locale = LocaleContextHolder.getLocale();
// String defaultDatePattern;
// try
// {
// defaultDatePattern = "dd/MM/yyyy";
// } catch(MissingResourceException mse)
// {
// defaultDatePattern = "dd/MM/yyyy";
// }
// return defaultDatePattern == null ? : defaultDatePattern;
return GlobalConstants.DATE_PATTERN;
}
/**
* This method attempts to convert an Oracle-formatted date
* in the form dd-MMM-yyyy to mm/dd/yyyy.
*
* @param aDate date from database as a string
* @return formatted string for the ui
*/
public static String getDate(Date aDate)
{
SimpleDateFormat df;
String returnValue = "";
if(aDate != null)
{
df = new SimpleDateFormat(getDatePattern());
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* This method generates a string representation of a date/time
* in the format you specify on input
*
* @param aMask the date pattern the string is in
* @param strDate a string representation of a date
* @return a converted Date object
* @throws java.text.ParseException
* @see java.text.SimpleDateFormat
*/
public static Date convertStringToDate(String aMask, String strDate)
throws ParseException
{
SimpleDateFormat df;
Date date;
df = new SimpleDateFormat(aMask);
if(log.isDebugEnabled())
{
log.debug("converting '" + strDate + "' to date with mask '"
+ aMask + "'");
}
try
{
date = df.parse(strDate);
} catch(ParseException pe)
{
//log.error("ParseException: " + pe);
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
}
/**
* This method returns the current date time in the format:
* MM/dd/yyyy HH:MM a
*
* @param theTime the current time
* @return the current date/time
*/
public static String getTimeNow(Date theTime)
{
return getDateTime(timePattern, theTime);
}
/**
* This method returns the current date in the format: MM/dd/yyyy
*
* @return the current date
*/
public static Calendar getToday()
{
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(getDatePattern());
// This seems like quite a hack (date -> string -> date),
// but it works ;-)
String todayAsString = df.format(today);
Calendar cal = new GregorianCalendar();
cal.setTime(convertStringToDate(todayAsString));
return cal;
}
/**
* This method generates a string representation of a date's date/time
* in the format you specify on input
*
* @param aMask the date pattern the string is in
* @param aDate a date object
* @return a formatted string representation of the date
* @see java.text.SimpleDateFormat
*/
public static String getDateTime(String aMask, Date aDate)
{
SimpleDateFormat df;
String returnValue = "";
if(aDate == null)
{
log.error("aDate is null!");
} else
{
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* This method generates a string representation of a date based
* on the System Property 'dateFormat'
* in the format you specify on input
*
* @param aDate A date to convert
* @return a string representation of the date
*/
public static String convertDateToString(Date aDate)
{
return getDateTime(getDatePattern(), aDate);
}
public static String convertDateToString(String format, Date aDate)
{
return getDateTime(format, aDate);
}
/**
* This method converts a String to a date using the datePattern
*
* @param strDate the date to convert (in format MM/dd/yyyy)
* @return a date object
*/
public static Date convertStringToDate(String strDate)
{
Date aDate = null;
try
{
if(log.isDebugEnabled())
{
log.debug("converting date with pattern: " + getDatePattern());
}
aDate = convertStringToDate(getDatePattern(), strDate);
} catch(ParseException pe)
{
log.error("Could not convert '" + strDate
+ "' to a date, throwing exception");
pe.printStackTrace();
}
return aDate;
}
/**
* Date Arithmetic function. Adds the specified (signed) amount of time to
* the given time field, based on the calendar's rules.
* <p/>
* For example, to subtract 5 days from a specific date, it can be achieved
* by calling: <p>
* DateUtil.add(date, Calendar.DATE, -5).
* <p/>
*
* @param date The date to perform the arithmetic function on
* @param field A Calendar constant to retrieve the field value from the Date
* object. Same as for {}.
* @param amount the amount of date or time to be added to the field
* @return The date as a result of the execution of the arithmetic function.
*/
public static Date add(Date date, int field, int amount)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(field, amount);
return cal.getTime();
}
/**
* This method will get the calendar information
* @param date
* @return Map Calendar.DAY_OF_WEEK:return day of week
* Calendar.WEEK_OF_MONTH:return week of Month
* Calendar.YEAR: return year
* Calendar.MONTH: return month
* Calendar.DATE: return date
* count: return date count
* position: return date position
*/
public static Map getCalenderInfo(Date date)
{
Map resultMap = new HashMap();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,-1);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE,1);
int weekOfMonth = cal.get(Calendar.WEEK_OF_MONTH);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH)+1;
int dt = cal.get(Calendar.DATE);
int count = getDateCount(convertWeekToDate(year,month,cal.get(Calendar.DAY_OF_WEEK)-1));
int position = getDatePosition(convertWeekToDate(year,month,cal.get(Calendar.DAY_OF_WEEK)-1),date);
resultMap.put(new Integer(Calendar.DAY_OF_WEEK),new Integer(dayOfWeek));
resultMap.put(new Integer(Calendar.WEEK_OF_MONTH),new Integer(weekOfMonth));
resultMap.put(new Integer(Calendar.YEAR),new Integer(year));
resultMap.put(new Integer(Calendar.MONTH),new Integer(month));
resultMap.put(new Integer(Calendar.DATE),new Integer(dt));
resultMap.put("count",new Integer(count));
resultMap.put("position",new Integer(position));
return resultMap;
}
private static Date convertWeekToDate(int year, int month, int week)
{
int monthWeek_int = 1;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.WEEK_OF_MONTH, monthWeek_int);
calendar.set(Calendar.DAY_OF_WEEK, week + 1);
if (calendar.get(Calendar.MONTH) != (month-1))
{
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.WEEK_OF_MONTH, monthWeek_int + 1);
} else
{
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.WEEK_OF_MONTH, monthWeek_int);
}
return calendar.getTime();
}
private static int getDateCount(Date inputDate)
{
int count = 1;
Calendar cal = Calendar.getInstance();
cal.setTime(inputDate);
int month = cal.get(Calendar.MONTH);
for (int i = 0; i < 6; i++)
{
cal.add(Calendar.DATE, 7);
if (cal.get(Calendar.MONTH) != month)
{
break;
} else {
count++;
}
}
return count;
}
private static int getDatePosition(Date inputDate,Date currentDate)
{
int count = 1;
Calendar cal = Calendar.getInstance();
cal.setTime(inputDate);
int month = cal.get(Calendar.MONTH);
for (int i = 0; i < 6; i++)
{
if(convertDateToString("dd/MM/yyyy",currentDate).equals(convertDateToString("dd/MM/yyyy",cal.getTime())))
{
break;
}
cal.add(Calendar.DATE, 7);
if (cal.get(Calendar.MONTH) != month)
{
break;
} else {
count++;
}
}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -