1024programmer Blog Various methods and formats for obtaining the current time of the system in Java_DracoTianlong’s Blog

Various methods and formats for obtaining the current time of the system in Java_DracoTianlong’s Blog

/**
 * 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; }

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/various-methods-and-formats-for-obtaining-the-current-time-of-the-system-in-java_dracotianlongs-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索