JAVA processing time

Source: Internet
Author: User

1. How to convert java. util. Date to java. SQL. Date?
Conversion:
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. How to convert the obtained java. SQL. Date to the year-month-day output?

Java. SQL. Date sd;

String dateTime = sd. toString ();
3. If you want to insert data to 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 the TO_DATE function.
For example, the existing ud
TO_DATE (new SimpleDateFormat (). format (ud, "yyyy-MM-dd HH: mm: ss "),
"YYYY-MM-DD HH24: MI: SS ")
Note that the representation format in java is different from the format provided by the database.

A practical example

SQL = "update tablename set timer = to_date ('" + t + "', 'yyyymmddhh24miss ') where ....."

The t variable here is similar to: 20051211131223

4. How to convert a string in the format of "yyyy-mm-dd" 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 ()),
Integer. parseInt (st. nextToken ()));

Similarities and differences between java. util. Date and java. SQL. Date

Java. SQL. Date, java. SQL. Time, and java. SQL. Timestamp are subclasses of java. util. Date (Packaging class ).

But why does data truncation occur when the value of java. SQL. Date type is inserted into the Date Field of the database?

Java. SQL. Date is the data type set to match SQL DATE. The normalized java. SQL. Date only contains the year, month, and day information, and the time, minute, and second milliseconds are cleared. The format is similar to YYYY-MM-DD. When we call the getDate () method of ResultSet to obtain the returned value, the java program uses java. SQL. Date of the "standard" to format the value in the database. Therefore, the information of the nonstandard part in the database will be stolen.

In the ResultSet. java provided by sun, comment on getDate as follows:
Retrieves the of 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 set a java. SQL. when the Date value is stored in the database through the setDate method of PrepareStatement, the java program will. SQL. date normalization, the nonstandard part will be stolen. However, we use java. SQL. date is generally set by java. util. date conversion, such as java. SQL. date sqlDate = new java. SQL. date (new java. util. date (). getTime ()).
Obviously, the converted java. SQL. Date is often not a standard java. SQL. Date. To save the exact value of java. util. Date,
We need to use java. SQL. Timestamp.

Calendar

Calendar calendar = Calendar. getInstance ();
// Obtain the current time and declare the time variable
Int year = calendar. get (Calendar. YEAR );
// Get the year
Int month = calendar. get (Calendar. MONTH );
// Get the month, but add 1 to the month
Month = month + 1;
Int date = calendar. get (Calendar. DATE );
// Obtain the date
String today = "" + year + "-" + month + "-" + 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.