Time Processing in Java

Source: Internet
Author: User
// Define a time format variable
Private Static final string default_pattern = "yyyymmddhhmmss ";

/**
* Author Hao xuewu
* Returns the time string in the specified format after the date is calculated.
*
* @ Param Interval
* Days
* @ Param Pattern
* Time Format
* @ Return
*/
Public static string getdate (string interval, date starttime, string pattern ){
Calendar temp = calendar. getinstance (timezone. getdefault ());
Temp. settime (starttime );
Temp. Add (temp. Month, integer. parseint (interval ));
Simpledateformat SDF = new simpledateformat (pattern );
Return SDF. Format (temp. gettime ());
}
/**
* Author Hao xuewu
* Convert string type to date type
* @ Return
*/
Public static date str2date (string Str ){
Date d = NULL;
Simpledateformat SDF = new simpledateformat (default_pattern );
Try {
D = SDF. parse (STR + "000000 ");
} Catch (exception e ){
E. printstacktrace ();
}
Return D;
}
/**
* Author Hao xuewu
* Format the time.
* @ Return
*/
Public static date datepattern (date ){
Simpledateformat SDF = new simpledateformat (default_pattern );
Try {
String dd = SDF. Format (date );
Date = str2date (dd );
} Catch (exception e ){
E. printstacktrace ();
}
Return date;
}
/**
* Author Hao xuewu
* Convert date to the corresponding string
* @ Return
*/
Public static string date2str (date ){
Simpledateformat SDF = new simpledateformat (default_pattern );
Return SDF. Format (date );
}
 

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.