Java:string and date types convert to each other __java

Source: Internet
Author: User

String converted to date type:
Method 1:
Also the simplest method date Date=new date ("2008-04-14");


Method 2:
SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");/lowercase MM indicates minutes
String dstr= "2008-4-24";
Java.util.Date Date=sdf.parse (DSTR);


Method 3:

public static Date stringtodate (String str) {  
	        dateformat format = new SimpleDateFormat ("Yyyy-mm-dd");  
	        Date date = null;  
	        try {  
	            //Fri Feb 00:00:00 CST  
	            date = Format.parse (str);   
	        } catch (ParseException e) {  
	            e.printstackt Race ();  
	        }  
	        2012-02-24  
	        date = java.sql.Date.valueOf (str);  
	                                              
	        return date;  
	    }  
Date startdate=stringtodate (starttime);


Date converted to string:

SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy-mm-dd");
Java.util.Date date=new java.util.Date ();
String Str=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.