Java.util.Date, Java.sql.Date, Java.sql.Time, java.sql.Timestamp differences and linkages

Source: Internet
Author: User
Tags time zones

Reference: Links
In web development, you can avoid the operation of dates, and there are several commonly used time-related classes:
java.util.Date, Java.sql.Date, Java.sql.Time, Java.sql.Timestamp, these classes are defined in the JDK as follows:
Java.lang.Object
..... |__java.util.date
..... |__java.sql.date/java.sql.timestamp/java.sql.time
..... |__java.security.timestamp
Java.util.Date date format is: Month Day time minutes seconds
Java.sql.Date date format: Month Day [Store date data does not store time data, is specifically designed for SQL]
Java.sql.Time date format: hours and seconds
Java.sql.Timestamp Date format: Day of the month and seconds (nanosecond)
The relationships of these classes are as follows:
Java.util.Date This class is the parent class of the Java.sql.Date, Java.sql.Time, java.sql.Timestamp three classes. These three classes are packaged with the Java.util.Date class.

The Java.sql.Date class masks the time-related methods of the Java.util.Date class (as in the form: HH:MM:SS), so it is not possible to pass information about this class access time, for example, if you go through the Sqldate.gethour () method to access the hour information, This method throws a IllegalArgumentException exception. This is because Java.sql.Date overrides the parent class when inheriting the Java.util.Date class, disabling the method of time access. The reason for this is to match the date data type of the database, and the date data class for the database is just the field that holds the dates. But it java.sql.Date class has the GetTime method to return the number of milliseconds, so it can be interchanged with java.util.Date:
Java.sql.Date converted to Java.util.Date
Java.sql.Date sqldate=new java.sql.Date ();
Java.util.Date utildate=new java.util.Date (Sqldate.gettime ());
Java.util.Date converted to Java.sql.Date
Java.util.Date utildate=new Date ();
Java.sql.Date sqldate=new java.sql.Date (Utildate.gettime ());

The Java.sql.Time class masks the date-related fields of the java.util.Date (form: YYYY-MM-DD), and therefore cannot pass information about this class access date, such as: If you pass Sqltime.getyear () method to get information about the year, this method throws a IllegalArgumentException exception. This is because Java.sql.Time overrides the parent class when inheriting the Java.util.Date class, disabling the method of date access. The reason for this is to match the time data type of the database, which is just the field in which the data class of the database is saved.

The

Java.sql.date is only included in the date. The java.sql.time contains only one time. Java.sql.time Java.sql.date How do they combine into a java.util.date? Take milliseconds to add, as the constructor of the Java.util.date parameter is possible.
Java.sql.Date d = new Java.sql.Date (new Java.util.Date (). GetTime ());
Java.sql.Time t = new Java.sql.Time (new Java.util.Date (). GetTime ());
Java.util.Date day = new Java.util.Date (d.gettime () + t.gettime ()); The
Java.sql.Timestamp field expands the Java.util.Date class by adding a millisecond of time access control to the Java.util.Date class, so you can get the number of nanoseconds in time by Getnanos method (note the time taken here is in nanoseconds, 1 seconds equals 1 billion nanoseconds, and again, this is to match the timestamp data type in the database.

What is the relationship between the Java.util.Date and the Java.util.Calendar class when the above four classes have been cleared?
The Java.util.Calendar class is a more in-depth, more comprehensive alternative to the Java.util.Date class. The Java.util.Calendar class supports all features of the java.util.Date, in addition, the calendar also introduces multi-lingual, multi-region features that can be used to get different regions, different time zones, and many other operations that are more convenient and fast than date, such as getting The first few weeks of the year, the number of days of each month, and other convenient methods.
Java.util.Calendar differences and java.util.Date several places also need to note: First, the calendar increases the millisecond time period, through which it can get the millisecond value of the point in time, and Java.util.Date is only accurate to the second. Second, the calendar gets the year when it is the current year, such as: 2010, and date gets the year with a value of -1900 for the current year (2010-1900=110, so after you call getyear, the previous value is 110). The last calendar is an abstract class that can be instantiated because the calendar here acts as a factory-like function, instantiating the calendar subclass GregorianCalendar in the getinstance method, and return it to the user for use.

Converting between Java

 PackageDemoImportJava.sql.Timestamp;ImportJava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Date; Public  class TestData {     Public Static void Main(string[] args)throwsparseexception {Date d =NewDate (NewDate (). GetTime ()); SimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String nowday1 ="2015-5-12 10:30:12"; String Nowday2 ="2015-05-12 10:30:12"; Timestamp TT =NewTimestamp (System.currenttimemillis ()); System.out.println (Sdf.format (d));//date, StringSystem.out.println (Sdf.parse (nowday1));//String-DateSystem.out.println (Sdf.format (TT));//Timestamp, StringSystem.out.println (tt.valueof (nowday2));//string-timestamp:string type must be shaped like: Yyyy-mm-dd hh:mm:ss[.f ...] in such a format, the brackets indicate optional, otherwise errorSystem.out.println (D=TT);//Timestamp-Date because the parent class is directly upward-shiftingSystem.out.println (NewTimestamp (D.gettime ()));//Date->timestamp}}/* * Date and TIMESTAMP conversion in Oracle * SELECT To_timestamp (to_char (sysdate, ' yyyy-mm-dd HH24:MI:SS '), ' Yyyy-mm-dd Hh24:mi: SS ') from dual; * SELECT to_date (To_char (To_timestamp (To_char (sysdate, ' yyyy-mm-dd HH24:MI:SS '), ' Yyyy-mm-dd HH24:MI:SS '), ' Yyyy-mm-dd HH24:MI:SS '), ' Yyyy-mm-dd HH24:MI:SS ') from dual; * *  *//**2015-05-12 10:44:15tue may 10:30:12 CST 20152015-05-12 10:44:152015-05-12 10:30:12.02015-05-12 10:44:15.1082015-05-12 10:44:15.108**/

Java.util.Date, Java.sql.Date, Java.sql.Time, java.sql.Timestamp differences and linkages

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.