Java Processing database date type data __ Database

Source: Internet
Author: User
1. valueof conversion time using the date class and the timestamp class
Date.valueof ();
Timestamp.valueof ();
If the two conversions are used directly on the SAL statement, then the outermost layer should be enclosed in double quotes or single quotes, such as insertion time: INSERT into value (' Date.valueof (2012-12-12) ');


2. Insert time in the database


PreparedStatement PS = con.preparestatement ("INSERT INTO TableName (Daddtime) VALUES (?)");
Here are three different ways:
1) ps.setdate (1,new java.sql.Date (System.currenttimemillis ()));
2) Ps.settime (2,new java.sql.Time (System.currenttimemillis ()));
3) Ps.settimestamp (3,new Java.sql.Timestamp (System.currenttimemillis ()));
The first type is only inserted into the month and 0000-00-00
Second insert time only 00:00:00
The third type inserts the full time 0000-00-00 00:00:00.000 000 is the number of milliseconds.




3.java.sql.date and Java.util.Date type conversions

public static Java.util.Date sqldatetoutildate (Java.sql.Date sdate)
{
Java.util.Date udate = null;
Long T = Sdate.gettime ();
Udate = new Date (t);
return udate;
}
public static Java.sql.Date utildatetosqldate (Java.util.Date udate)
{
Java.sql.Date sdate = null;
Long T = Udate.gettime ();
Sdate = new Java.sql.Date (t);
return sdate;
}

public static void Main (string[] args) {

Date date = new Date ();
SYSTEM.OUT.PRINTLN ("date-turn string:" + convertdate.datetostr (date));
System.out.println ("String Turn Date:" + convertdate.strtodate (convertdate.datetostr (date));

}


4. Common Java Program conversions
String turn to date
public static void Datatest () {
SimpleDateFormat format = new SimpleDateFormat ("2012-12-12 00:00:00");
Date LastModifyTime;
try {
LastModifyTime = Format.parse ("2012-12-12 00:00:00");
System.out.println (Lastmodifytime.tolocalestring ());
catch (ParseException e) {
E.printstacktrace ();
}
}


Date turns string
public static void TestData () {
try {
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
System.out.println (Sdf.format ("2012-12-12 01:12:11"));
System.out.println (Java.sql.Timestamp.valueOf ("2012-12-12 01:12:11"));
catch (Exception 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.