String & Timestamp

Source: Internet
Author: User
Package COM. apple. sqm. common; import Java. SQL. timestamp; import Java. text. simpledateformat; import Java. util. calendar; import Java. util. date; import Java. util. timezone;/*** @ author junbos **/public class datehelper {Private Static final string default_pattern = "yyyymmddhhmmss "; /*** @ Param time * @ Param pattern * @ return */public static string timestamp2str (timestamp time, string pattern) {If (Ti Me = NULL) {Throw new illegalargumentexception ("timestamp is null");} If (pattern! = NULL &&! "". Equals (pattern) {If (! "Yyyymmddhhmmss". Equals (pattern )&&! "Yyyy-mm-dd hh: mm: SS". Equals (pattern )&&! "Yyyy-mm-dd". Equals (pattern )&&! "Mm/DD/YYYY ". equals (pattern) {Throw new illegalargumentexception ("Date Format [" + pattern + "] is invalid") ;}} else {pattern = default_pattern;} calendar Cal = calendar. getinstance (timezone. getdefault (); Cal. settime (time); simpledateformat SDF = new simpledateformat (pattern); Return SDF. format (Cal. gettime ();} public static timestamp str2timestamp (string timestr, string pattern) {timestamp result = NULL; If (timestr = NULL) {Throw new illegalargumentexception ("timestamp is null");} If (pattern! = NULL &&! "". Equals (pattern) {If (! "Yyyymmddhhmmss". Equals (pattern )&&! "Yyyy-mm-dd hh: mm: SS". Equals (pattern )&&! "Mm/DD/YYYY hh: mm: SS". Equals (pattern )&&! "Yyyy-mm-dd". Equals (pattern )&&! "Mm/DD/YYYY ". equals (pattern) {Throw new illegalargumentexception ("Date Format [" + pattern + "] is invalid") ;}} else {pattern = default_pattern;} date d = NULL; simpledateformat SDF = new simpledateformat (pattern); try {d = SDF. parse (timestr); Result = new timestamp (D. gettime ();} catch (exception e) {e. printstacktrace ();} return result;} public static void main (string [] ARGs) {system. out. println (str2timestamp ("13:10:00", "yyyy-mm-dd hh: mm: SS"); system. out. println (timestamp2str (New timestamp (system. currenttimemillis (), "yyyy-mm-dd hh: mm: SS"); system. out. println (str2timestamp ("01/25/2008", "mm/DD/YYYY "));}}

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.