The date type and string type of JavaSE are converted to each other.

Source: Internet
Author: User

The date type and string type of JavaSE are converted to each other.

Package cn. zwq. convert; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; /*** convert Date and String types ** @ author zwq * @ version V1.0 * @ Date 8:28:04 on January 1, October 11, 2016 **/public class DateConvert {public static void main (String [] args) {Date date = stringToDate ("1979-01-01"); // Date date = stringToDate ("19790101"); // Date date = stringToDate ("1979.01.01 "); // Date date = stringToDate ("1979/01/01"); // Date date = stringToDate ("January 01, 1979"); String string = dateToString (date); System. out. println (string);} public static Date stringToDate (String string) {SimpleDateFormat [] simpleDateFormats = {new SimpleDateFormat ("yyyy-MM-dd "), new SimpleDateFormat ("yyyyMMdd"), new SimpleDateFormat ("yyyy. MM. dd "), new SimpleDateFormat (" yyyy/MM/dd "), new SimpleDateFormat (" yyyy MM dd ")}; for (int I = 0; I <simpleDateFormats. length; I ++) {try {Date date = simpleDateFormats [I]. parse (string); return date;} catch (ParseException e) {continue ;}return null;} public static String dateToString (Date date) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("MM dd, yyyy"); String string = simpleDateFormat. format (date); return string ;}}

 

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.