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

Source: Internet
Author: User
Tags time zones

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

section: Java Foundation Author:admin Date:2015-04-19 Comments:0 Hits: 1,276 times

Java.util.Date, Java.sql.Date, Java.sql.Time, and Java.sql.Timestamp, which are defined in the JDK as follows:
java.lang.object 
..... | __JAVA.UTIL.DATE&NBSP,
..... | __java.sql.date/java.sql.timestamp/java.sql.time
..... | __java.security.timestamp
Java.util.Date date format is: Year Month day time  
java.sql.Date date format: Month day [only date data stored does not store time data, is designed specifically for SQL design] 
Java.sql.Time date format: time division seconds  
Java.sql.Timestamp date format: Month Day minute seconds nanosecond (nanosecond)
The relationships of these classes are as follows:
Java.util.Date This class is java.sql.date,  java.sql.time,  Java.slq.Timestamp the parent class of these three classes. These three classes are packaged with the Java.util.Date class.

java.sql.date class blocks Java . util. The time-related method of the date Class (shape: HH:MM:SS), therefore, cannot be accessed through this class of access time information, 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 a gettime method that returns the number of milliseconds, so it can be interchanged with java.util.Date:
Java.sql.Date 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 to Java.sql.Date
java.util.Date utildate=new Date ();
Java.sql.Date sqldate=new java.sql.Date (Utildate.gettime ());

java.sql.time class blocks Java . util. Date-related fields (shape: YYYY-MM-DD), therefore, cannot pass information about this class access date, such as: If you go through the 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.

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 when it gets the value of the current year-1900 (2010-1900=110, so after you call getyear, the past 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.


Source: http://swiftlet.net/archives/754

From for notes (Wiz)

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.