/**
* Return default datePattern (MM/dd/yyyy)
* @return a string representing the date pattern on the UI
*/
public static String getDatePattern() {
return datePattern;
}
/**
* 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 final String getDate(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(datePattern);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final String date2Str(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(datePattern);
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final String date2Str(String pattern, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(pattern);
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
* @see java.text.SimpleDateFormat
* @throws ParseException
*/
public static final Date convertStringToDate(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
public static final Date str2Date(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
/**
* 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
* @throws ParseException
*/
public static Calendar getToday() throws ParseException {
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(datePattern);
// 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 final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
System.out.print("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 final String convertDateToString(Date aDate) {
return getDateTime(datePattern, 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
*
* @throws ParseException
*/
public static Date convertStringToDate(String strDate)
throws ParseException {
Date aDate = null;
try {
aDate = convertStringToDate(datePattern, strDate);
} catch (ParseException pe) {
//log.error("Could not convert ‘" + strDate
// + "‘ to a date, throwing exception");
pe.printStackTrace();
return null;
}
return aDate;
}
//日期格式转换成时间戳
public static long getTimeStamp(String pattern, String strDate) {
long returnTimeStamp = 0;
Date aDate = null;
try {
aDate = convertStringToDate(pattern, strDate);
} catch (ParseException pe) {
aDate = null;
}
if (aDate == null) {
returnTimeStamp = 0;
} else {
returnTimeStamp = aDate.getTime();
}
return returnTimeStamp;
}
//获取当前日期的邮戳
public static long getNowTimeStamp() {
long returnTimeStamp = 0;
Date aDate = null;
try {
aDate = convertStringToDate("yyyy-MM-dd HH:mm:ss", getNowDateTime());
} catch (ParseException pe) {
aDate = null;
}
if (aDate == null) {
returnTimeStamp = 0;
} else {
returnTimeStamp = aDate.getTime();
}
return returnTimeStamp;
}
/**
*得到格式化后的系统当前日期
*@param strScheme 格式模式字符串
*@return 格式化后的系统当前时间,如果有异常产生,返回空串""
*@see java.util.SimpleDateFormat
*/
public static final String getNowDateTime(String strScheme) {
String strReturn = null;
Date now = new Date();
try {
SimpleDateFormat sdf = new SimpleDateFormat(strScheme);
strReturn = sdf.format(now);
} catch (Exception e) {
strReturn = "";
}
return strReturn;
}
public static final String getNowDateTime() {
String strReturn = null;
Date now = new Date();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
strReturn = sdf.format(now);
} catch (Exception e) {
strReturn = "";
}
return strReturn;
}