Java各種擷取系統目前時間方法 和 格式

來源:互聯網
上載者:User
/** * 返回當前日期時間字串<br> * 預設格式:yyyy-mm-dd hh:mm:ss *  * @return String 返回當前字串型日期時間 */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;}/** * 返回當前日期時間字串<br> * 預設格式:yyyymmddhhmmss *  * @return String 返回當前字串型日期時間 */public static BigDecimal getCurrentTimeAsNumber() {String returnStr = null;SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");Date date = new Date();returnStr = f.format(date);return new BigDecimal(returnStr);}/** * 返回自訂格式的當前日期時間字串 *  * @param format *            格式規則 * @return String 返回當前字串型日期時間 */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 String 返回的字串型日期 */public static String getCurDate() {Calendar calendar = Calendar.getInstance();SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");String strDate = simpledateformat.format(calendar.getTime());return strDate;}/** * 返回指定格式的字元型日期 * @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;}/** * 返回當前字串型日期 *  * @param format *            格式規則 *  * @return String 返回的字串型日期 */public static String getCurDate(String format) {Calendar calendar = Calendar.getInstance();SimpleDateFormat simpledateformat = new SimpleDateFormat(format);String strDate = simpledateformat.format(calendar.getTime());return strDate;}/** * 返回TimeStamp對象 *  * @return */public static Timestamp getCurrentTimestamp() {Object obj = TypeCaseHelper.convert(getCurrentTime(), "Timestamp", "yyyy-MM-dd HH:mm:ss");if (obj != null)return (Timestamp) obj;elsereturn null;}/** * 將字串型日期轉換為日期型 *  * @param strDate *            字串型日期 * @param srcDateFormat *            源日期格式 * @param dstDateFormat *            目標日期格式 * @return Date 返回的util.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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.