How to convert a String type date format to YYYYMMDD to the date type YYYY-MM-DD format in Java

Source: Internet
Author: User

Today when doing the WebService interface service, the XML file is passed into a node for the person's birth date, the corresponding entity class field type is the date type, the value obtained is string type, the code is as follows:

                    Gets a value of "19570323" String datestring = Emp.gethealthcareprincipalperson (). Getbirthtime (). GetValue
		    
		    (). toString ();
                    1. Define conversion format SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd");
		    SimpleDateFormat formatter2 = new SimpleDateFormat ("YyyyMMdd"); 2, call Formatter2.parse (), convert "19570323" to date type output as: Sat Mar 00:00:00 gmt+08:00 1957 date = Formatter2.parse (
		    
		   datestring);
		    3. Convert Date type (Sat 00:00:00 gmt+08:00 1957) to String type//Note now formatter is used for conversion and output is of type string: "1957-03-23"
		    
		    String dstring = Formatter.format (date);
                   4. Converting a string to date requires notice that the java.sql.Date.valueOf () function can only accept Date data = Java.sql.Date.valueOf (dstring) with a parameter type of YYYY-MM-DD type; 5. Assigns the date-type birthdate to JavaBean personaudit.setbirthdate (Emp.gethealthcareprincipalp Erson ()!= null && Emp.gethealthcareprincipalperson (). Getbirthtime ()!= null)? data:null); 




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.