Java methods and formats for obtaining the current time of the system

Source: Internet
Author: User
/*** Returns the current date and time string <br> * default format: yyyy-mm-dd hh: mm: SS ** @ return string returns the current string date and time */public static string getcurrenttime () {string returnstr = NULL; simpledateformat F = new simpledateformat ("yyyy-mm-dd hh: MM: SS "); Date = new date (); returnstr = f. format (date); Return returnstr;}/*** returns the current date and time string <br> * default format: yyyymmddhhmmss ** @ return string returns the current string date and time */public static bigdecimal getcurrenttimeasnum BER () {string returnstr = NULL; simpledateformat F = new simpledateformat ("yyyymmddhhmmss"); Date = new date (); returnstr = f. format (date); return New bigdecimal (returnstr );} /*** return the custom format of the current date and time string ** @ Param Format * format rule * @ return string returns the current string date and time */public static string getcurrenttime (string format) {string returnstr = NULL; simpledateformat F = new simpledateformat (format); Date = new DA Te (); returnstr = f. format (date); Return returnstr;}/*** returns the current string date ** @ return string returns the string date */public static string getcurdate () {calendar = calendar. getinstance (); simpledateformat = new simpledateformat ("yyyy-mm-dd"); string strdate = simpledateformat. format (calendar. gettime (); Return strdate;}/*** return the sorted date * @ Param formatstring * @ return */Public ST in the specified format Atic string date2string (date, string formatstring) {If (g4utils. isempty (date) {return NULL;} simpledateformat = new simpledateformat (formatstring); string strdate = simpledateformat. format (date); Return strdate ;} /*** return the current string date ** @ Param Format * format rule ** @ return string returned string date */public static string getcurdate (string format) {calendar = calendar. getinstance (); simp Ledateformat simpledateformat = new simpledateformat (format); string strdate = simpledateformat. format (calendar. gettime (); Return strdate;}/*** returns 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; elsereturn NULL ;} /*** convert string date to date ** @ Param strdate * string date * @ Param srcdateformat * Source date format * @ Param dstdateformat * target date format * @ return util. Returned by date. 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 t Mpstring = NULL; If (tmpdate! = NULL) tmpstring = (New simpledateformat (dstdateformat). Format (tmpdate); If (tmpstring! = NULL) rtdate = (New simpledateformat (dstdateformat). parse (tmpstring, new parseposition (0); Return rtdate ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.