The difference between date,calendar,timestamp in Java and the conversion and use of _java

Source: Internet
Author: User
Tags dateformat getdate string format oracle database

1 Java.util.Date
Contains the year, month, day, time, minute, and second information.

Copy Code code as follows:

string is converted to date
String datestr= "2013-8-13 23:23:23";
String pattern= "Yyyy-mm-dd HH:mm:ss";
Dateformate dateformat=new SimpleDateFormat (pattern);
Date Date=dateformat.parse (DATESTR);
Date=dateformat.format (date);

2 Java.sql.Date
Contains the year, month, and day information.
Inherited from Java.util.Date. Used in database related operations, such as Rs.getdate,ps.setdate. RS refers to the Resultset,ps refers to the PreparedStatement.
Copy Code code as follows:

Java.util.Date conversion to Java.sql.Date
New Java.sql.Date (Utildate.gettime ());//Utildate An object of java.util.Date type

3 Java.util.Calendar
Contains the year, month, day, hour, minute, second, and millisecond information.
JDK1.1 introduced to replace Java.util.Date.
Copy Code code as follows:

Date to Calendar
Date Date=new date ();
Calendar calendar=calendar.getinstance ();
Calendar.settime (date);

Calendar converted to date
Calendar ca=calendar.getinstance ();
Date d = (date) ca.gettime ();


4 Java.sql.Timestamp
Includes year, month, day, time, minutes, seconds, nanosecond (Nano)Information.
Inherited from Java.util.Date. Contains more information than Java.sql.Date. Used in database related operations, such as Rs.gettimestamp,ps.settimestamp. For example, if a field in a database is hiredate to the date type of Oracle, the year, month, day, time, minute, and second information can be taken out using Gettimestamp, but only the year, month, and day information can be retrieved when using GETDATE. Therefore, the use of Gettimestamp is generally recommended.
Copy Code code as follows:

Java.util.Calendar conversion to Java.sql.Timestamp
New Timestamp (Calendar.getinstance (). Gettimeinmillis ());
Java.util.Date conversion to Java.sql.Timestamp
New Timestamp (Date.gettime ());
String to java.sql.timestamp,string format: Yyyy-mm-dd hh:mm:ss[.f ...], square brackets indicate optional
Timestamp.valueof ("2013-07-06 01:49:30");

5 Date and time types provided by Oracle database
The Oracle database provides four types of Date,timestamp,timestamp with time zone and TIMESTAMP with a local-time zone.

Date contains century, year, month, day, time, minute, and second information.
Timestamp is a date extension that contains the year, month, day, time, minute, second, and fractional seconds information. The format for defining timestamp is as follows:

Copy Code code as follows:

TIMESTAMP [(Fractional_seconds_precision)]
Format
TIMESTAMP ' Yyyy-mm-dd HH24:MI:SS. FF '
An example
TIMESTAMP ' 1997-01-31 09:26:50.12 '

Where fractional_seconds_precision is optional, used to specify that a floating-point number with several decimal digits is used to indicate that the value range is 0 to 9, and the default is 6. In the example above, the two-bit decimal is used, and its second value is 50.12. Note: 12 is not a millisecond value, nor is it a microsecond value.

Related Article

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.