The conversion of date in Java to DateTime, string, and date __java

Source: Internet
Author: User

This transformation is often met in the project and is briefly described:

(1) Date converted to datetime

DateTime corresponds to the timestamp in the Java database and is converted as follows:

		Java.util.Date Convert to timestamp
		date = new Date ();
		Timestamp Timestamp = new Timestamp (Date.gettime ()); 2013-01-14 22:45:36.484


(2) string converted to date

		String is converted to java.util.Date
		string str = "2013-01-14";
		SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
		Date date = null; Initializes the date
		try {
			date = Sdf.parse (str);//mon the 00:00:00 CST 2013
		} catch (ParseException e) {
			E.prin Tstacktrace ();
		}


(3) Date converted to string

		Java.util.Date Convert to string
		date = new Date ();
		DateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
		String str = format.format (date); 2013-01-14

(4) string converted to Java.sql.Date

Convert to java.sql.Date The purpose is to manipulate the database

		String is converted to java.sql.Date
		string str = "2013-01-14";
		SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
		Java.sql.Date sdate = null; Initializes the
		try {
			java.util.Date udate = sdf.parse (str);
			Sdate = new Java.sql.Date (Udate.gettime ()); 2013-01-14
		} catch (ParseException e) {
			e.printstacktrace ();
		}




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.