Small Example of converting string to date in Java

Source: Internet
Author: User

Scenario: There is a string that needs to be converted to date, for example, "Fri Jun 08 15:22:40 CST 2012 ".

Solution: Use the parse () method of simpledateformate

ExampleCode:

Import Java. text. dateformat; import Java. text. parseexception; import Java. text. simpledateformat; import Java. util. date; import Java. util. locale; import Java. util. timezone; public class string2date {public static void main (string [] ARGs) {string STR = "Fri Jun 08 15:22:40 CST 2012"; Date mydate = new date (); simpledateformat SDF = new simpledateformat ("eee Mmm dd hh: mm: ss z yyyy", locale. english); try {mydate = SDF. parse (STR);} catch (parseexception e) {e. printstacktrace ();} system. out. println ("mydate:" + mydate. tostring ());}}

For the construction method of the first parameter in simpledateformate, see the JDK documentation, which contains a list and many examples.

Link: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

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.