Java Date processing (Timestamp)

Source: Internet
Author: User
Tags dateformat time and seconds

To write some date processing with the database connection, the type of pstmt.setdate () is the java.sql.Date type, and this type of conforming specification does not actually save the time and seconds into the database, so the access should be Settimestamp () or Gettimestamp ().

Finishing One:

First, the date data stored in the database

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 ()));

Ii. fetching data from the database
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 ());
Note:
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 ~

Finishing two:
It is convenient to use timestamp to record datetime, but sometimes it is not necessary to display the milliseconds after the decimal point, so you need to redefine the format when converting to string.
timestamp converted to string:

New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"//  new// = Df.format (now);       

Convert String to timestamp:

New SimpleDateFormat ("Yyyy-mm-ddhh:mm:ss"= Df.format (new= timestamp.valueof (time);   

Finishing three:

The data type of setdate or getdate that we often use in resultset is java.sql.Date, and we are generally accustomed to using java.util.Date in our usual Java programs. So in the DAO layer we often encounter the mutual conversion of these two data types.
The relationship between the two

Java.lang.Object
|
+---Java.util.Date
|
+----Java.sql.Date

Convert each other
1. Using the gettime () function

These two classes all provide the gettime () function, which returns the corresponding number of milliseconds (long type). This function can be used to implement transformations:

Java.util.Date utildate = new Java.util.Date (Sqldate.gettime ()); SQL-Util

Java.sql.Date sqldate = new Java.sql.Date (Utildate.gettime ()); Util-SQL
2. Implementing conversions using the SimpleDateFormat class
SimpleDateFormat is a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date-and-text), parsing (text, date), and normalization.

SimpleDateFormat DateFormat = new Simpledateformate ("Yyyy-mm-dd HH:mm:ss");

Java.util.Date utildate = Dateformat.parse (sqldate.tostring ());
3. Direct conversion
Since Java.sql.Date is inherited from the java.util.Date, it can be used directly: Utildate = sqldate;

4. Alternative method of acquiring dates:

SimpleDateFormat sy=New SimpleDateFormat ("yyyy"); SimpleDateFormat sm=new simpledateformat ("MM"); SimpleDateFormat SD=new SimpleDateFormat ("DD"); String syear=Sy.format (date); String Smon=Sm.format (date);  String sday=Sd.format (date); 

Ps:1. The getyear () in the Java.util.Date class is added with 1900 to get the actual value, and the GetMonth () is added 1.
2. String-to-date conversion: date.valueof (str), remember the date packet to be cited for SQL when the primer is not a util date packet
3. String-to-Timestamp conversion Timestamp ts = timestamp.valueof (time);

Java Date processing (Timestamp)

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.