Java Tutorial: Convert string to date

Source: Internet
Author: User

Example 1: String dateStr = "06:23:12 ";

Date date = new Date ();

SimpleDateFormat strToDate = new SimpleDateFormat ("yyyy-MM-dd-hh: mm: ss ");
// Parse format String to date
Date = strToDate. parse (dateStr );

System. out. println (date );

// Date convert to format String according the format String
SimpleDateFormat dateToStr = new SimpleDateFormat ("dd/MM/yyyy-hh: mm: ss ");
// Parse date to format String

String temp = dateToStr. format (date );
System. out. println (temp );


Example 2:

/**

* Get Date

* @ Param dateStr

* @ Return

*/

String dateStr = "Thu Aug 05 12:56:00 CST 2010 ";

Private Date getExaminationDate (String dateStr ){

Date date = new Date ();

Try {

Date = new SimpleDateFormat (). parse (examinationDate );

} Catch (ParseException e ){

E. printStackTrace ();

}

Return date;

}

 

From http://www.jinflex.com/index.php/archives/86

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.