The difference between the date in Android Java.util.Date and Java.util.sql

Source: Internet
Author: User

1. Convert Java.util.Date to Java.sql.Date

Java.sql.Date SD;

Java.util.Date ud;

Initialize the UD such as UD = new Java.util.Date ();

SD = new Java.sql.Date (Ud.gettime ());

2. To insert into the database and the corresponding field is of the date type

You can use the preparedstatement.setdate (int, java.sql.Date) method

The java.sql.Date can be obtained using the above method

You can also use the database to provide to_date functions

such as existing UD

To_date (New SimpleDateFormat (). Format (UD, "Yyyy-mm-dd HH:mm:ss"),

"Yyyy-mm-dd HH24:MI:SS")

Note that in Java, the format differs from the format provided by the database

Sql= "UPDATE tablename set Timer=to_date ('" +x+ "', ' Yyyymmddhh24miss ') where ..."

The x here is similar to the variable: 20080522131223

3. How to convert a string in "YYYY-MM-DD" format to Java.sql.Date

Method 1

SimpleDateFormat Bartdateformat = new SimpleDateFormat ("Yyyy-mm-dd");

String datestringtoparse = "2007-7-12";

try{

Java.util.Date Date = Bartdateformat.parse (datestringtoparse);

Java.sql.Date sqldate = new Java.sql.Date (Date.gettime ());

System.out.println (Sqldate.gettime ());

}

catch (Exception ex) {

System.out.println (Ex.getmessage ());

}

Method 2

String strdate = "2002-08-09";

StringTokenizer st = new StringTokenizer (strdate, "-");

Java.sql.Date Date = new Java.sql.Date (Integer.parseint (St.nexttoken ()), Integer.parseint (St.nexttoken ()), Integ Er.parseint (St.nexttoken ()));

The difference between java.util.Date and java.sql.Date

Java.sql.date,java.sql.time and Java.sql.Timestamp Three are subclasses of Java.util.Date (wrapper class).

But why data interception occurs when a value of type java.sql.Date is inserted into the Date field in the database?

Java.sql.Date is the data type that is set to match SQL date. The "normalized" java.sql.Date contains only the month-date information, and seconds and minutes are zeroed. Format similar to: YYYY-MM-DD. When we call ResultSet's getdate () method to get the return value, the Java program formats the values in the database with reference to the java.sql.Date of the specification. Therefore, if the information in the non-normalized part of the database exists, it will be robbed.

This getdate is commented on in the sun-provided Resultset.java:

Retrieves the the designated column in the current row of this <code>ResultSet</code> object as a "java.sql . Date "Object in the Java programming language.

Similarly. If we put a java.sql.Date value through the Preparestatement setdate method into the database, the Java program will normalize the incoming java.sql.Date, the denormalized part will be robbed. However, we java.sql.Date generally converted by java.util.Date, such as: Java.sql.Date sqldate=new java.sql.Date (New Java.util.Date (). GetTime ()) .

Obviously, such conversion of java.sql.Date is often not a normative java.sql.Date. To save the exact value of the java.util.Date,

We need to use Java.sql.Timestamp.

Use ResultSet's gettimestamp to obtain data so that no interception issues occur

(This section is detailed in another article: Timestamp basic knowledge and time-size comparison)

Java.sql.Date only store date data does not store time data
Time data is lost
Preparedstatement.setdate (1, New Java.sql.Date (Date.gettime ()));
You can handle this.
Preparedstatement.settimestamp (1, New Timestamp (New Java.util.Date (). GetTime ()));

To get the complete data, including the date and time, you can
Java.util.Date d = resultset.gettimestamp (1);
This is a more appropriate process to avoid some potential timestamp problems
Java.util.Date d = new Java.util.Date (Resultset.gettimestamp (1). GetTime ());
You can receive the Java.util.Date type when you store the database and then use the GetTime () method to get a long value representing that date object, and then construct a timestamp object into the database with the Long value.
From the storage database, you can get timestamp with his gettime () method to get a long value, and then the long value to construct a Java.util.Date object, so that the date object can be manipulated. For example, New SimpleDateFormat ("Yyyyy-mm-dd HH:mm:ss"). Format (Date) or format (Timestamp) line ~

Calendar:

Calendar calendar=calendar.getinstance ();

Get current time, declare time variable

int Year=calendar.get (calendar.year);

Get year

int Month=calendar.get (calendar.month);

Get the month, but the month to add 1

month=month+1;

int Date=calendar.get (calendar.date);

Date obtained

String today= "+year+"-"+month+"-"+date+" ";

Reprint: http://blog.chinaunix.net/uid-21227800-id-65899.html

The difference between the date in Android Java.util.Date and Java.util.sql

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.