/**
* Returns the current datetime string
* Default format: yyyy-mm-dd hh:mm:ss
*
* @return String returns the current string type date time
*/
public static String getCurrentTime() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
returnStr = f. format(date);
return returnStr;
}
/**
* Returns the current datetime string
* Default format: yyyymmddhhmmss
*
* @return String returns the current string type date time
*/
public static BigDecimal getCurrentTimeAsNumber() {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
returnStr = f. format(date);
return new BigDecimal(returnStr);
}
/**
* Returns the current datetime string in a custom format
*
* @param format
* format rules
* @return String returns the current string type date time
*/
public static String getCurrentTime(String format) {
String returnStr = null;
SimpleDateFormat f = new SimpleDateFormat(format);
Date date = new Date();
returnStr = f. format(date);
return returnStr;
}
/**
* Return the current string date
*
* @return String Returned string date
*/
public static String getCurDate() {
Calendar calendar = Calendar. getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
String strDate = simpledateformat. format(calendar. getTime());
return strDate;
}
/**
* Returns a character date in the specified format
* @param date
* @param formatString
* @return
*/
public static String Date2String(Date date, String formatString) {
if (G4Utils. isEmpty(date)) {
return null;
}
SimpleDateFormat simpledateformat = new SimpleDateFormat(formatString);
String strDate = simpledateformat. format(date);
return strDate;
}
/**
* Return the current string date
*
* @param format
* format rules
*
* @return String Returned string date
*/
public static String getCurDate(String format) {
Calendar calendar = Calendar. getInstance();
SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
String strDate = simpledateformat. format(calendar. getTime());
return strDate;
}
/**
* Return the TimeStamp object
*
* @return
*/
public static Timestamp getCurrentTimestamp() {
Object obj = TypeCaseHelper. convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");
if (obj != null)
return (Timestamp) obj;
else
return null;
}
/**
* Convert a string date to a date
*
* @param strDate
* string date
* @param srcDateFormat
* source date format
* @param dstDateFormat
* target date format
* @return Date Returned util.Date type date
*/
public static Date stringToDate(String strDate, String srcDateFormat, String dstDateFormat) {
Date rtDate = null;
Date tmpDate = (new SimpleDateFormat(srcDateFormat)).parse(strDate, new ParsePosition(0));
String tmpString = null;
if (tmpDate != null)
tmpString = (new SimpleDateFormat(dstDateFormat)).format(tmpDate);
if (tmpString != null)
rtDate = (new SimpleDateFormat(dstDateFormat)). parse(tmpString, new ParsePosition(0));
return rtDate;
}