Java. util. Date, java. SQL. Date, java. SQL. Time, java. SQL. Timestamp difference and contact, java. SQL. timestamp
Reference: Link
In Web development, date operations cannot be avoided. There are several commonly used time classes:
Java. util. Date, java. SQL. Date, java. SQL. Time, java. SQL. TimestampThese classes are defined in JDK as follows:
Java. lang. Object
.... |__ Java. util. Date
.......... |__ Java. SQL. Date/java. SQL. Timestamp/java. SQL. Time
.... |__ Java. security. Timestamp
Java. util. Date Format: year, month, day, hour, minute, second
The format of java. SQL. Date is: year, month, and day. [only Date data is stored, but not time data is stored. It is specially designed for SQL]
Java. SQL. Time Date Format: hour, minute, and second
Java. SQL. Timestamp Date Format: year, month, day, hour, minute, second, nanosecond (in milliseconds)
The relationships between these classes are as follows:
Java. util. Date is the parent class of java. SQL. Date, java. SQL. Time, java. SQL. Timestamp. These three classes encapsulate the java. util. Date class.
Java. SQL. the Date class shields java. util. date-related methods (such as hh: mm: ss). Therefore, time-related information cannot be accessed through this class. For example, if you use sqlDate. the getHour () method is used to access the hour information. This method throws an IllegalArgumentException exception. This is because when java. SQL. Date inherits the java. util. Date class, it overwrites the parent class and disables the time access method. The reason for this processing is to match the Date data type of the database. The Date data class of the database is only a field related to saving the Date. However, the java. SQL. Date class has the getTime method that returns the number of milliseconds, so it can be exchanged with java. util. Date:
Convert 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 ());
Convert 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. the Time class shields java. util. date-related fields (such as yyyy-MM-dd). Therefore, you cannot use this class to access Date-related information. For example, if you use sqlTime. the getYear () method gets information about the Year. This method throws an IllegalArgumentException exception. This is because when java. SQL. Time inherits the java. util. Date class, it overwrites the parent class and disables the Date access method. The reason for this processing is to match the Time data type of the database. The Time data row of the database is only a field related to the storage Time.
Java. SQL. date only contains dates. Java. SQL. time only contains one time. How to combine java. SQL. time java. SQL. date into a java. util. date? The sum of milliseconds is used as the constructor parameter of java. util. date.
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 ());
Java. SQL. the Timestamp field is. util. the Date class is expanded, which is in java. util. the Date class adds time access control in milliseconds. Therefore, you can use the getNanos method to obtain the time in milliseconds (note that the time obtained here is in the unit of milliseconds, 1 second equals to one billion ml). Similarly, this is to match the Timestamp data type in the database.
After clarifying the relationships between the above four classes, what are the relationships between java. util. Date and java. util. Calendar classes?
Java. util. CalendarClass is a more in-depth and comprehensive alternative to the java. util. Date class. Java. util. the Calendar class supports java. util. all functions of Date. In addition, Calendar also introduces the multi-language and multi-region features. You can obtain the time of different regions and time zones as needed, calendar ar also adds many more convenient and convenient operations than Date, such as obtaining the day of the week and day of each month in a year.
Java. util. differences Between Calendar and java. util. pay attention to the following aspects of Date: first, Calendar increases the period of time in milliseconds, through which the millisecond value of the time point can be obtained, while java. util. date is only accurate to seconds. Second, the current year is obtained by the Calendar AR, for example, 2010, and the year obtained by Date is the current year.-1900The value (2010-1900 = 110, so the past value after you call getYear is 110 ). The last Calendar is an abstract class that can be instantiated because the Calendar here acts as a factory-like function.GetInstanceThe method instantiates the Calendar subclass GregorianCalendar and returns it to the user.
Conversion between Java
Package demo; import java. SQL. timestamp; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; public class TestData {public static void main (String [] args) throws ParseException {Date d = new Date (). getTime (); SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); String nowday1 = "2015-5-12 10:30:12 "; string nowday2 = "10:30:12"; Timestamp tt = new Timestamp (System. currentTimeMillis (); System. out. println (sdf. format (d); // Date-> String System. out. println (sdf. parse (nowday1); // String-> Date System. out. println (sdf. format (tt); // Timestamp-> String System. out. println (tt. valueOf (nowday2); // String-> Timestamp: the String type must be in the format of yyyy-mm-dd hh: mm: ss [. f...] in this format, brackets indicate optional; otherwise, System is reported. out. println (d = tt); // Timestamp-> Date because it is a parent class, the System is directly transformed up. out. println (new Timestamp (d. getTime (); // Date-> Timestamp}/** convert Date and Timestamp 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; *** // *** 10: 44: 15Tue May 12 10:30:12 CST 20152015-05-12 10:30:12 :44:152015-05-12. 02015-05-12 10:44:15. 1082015-05-12 10:44:15. 108 **/