Conversion of date and SQL in Java and Java. util. Date, java. util. Calendar, java. SQL. Date, java. SQL. Time, java. SQL. Times

Source: Internet
Author: User
Tags time zones time in milliseconds


Convert a string in the format of "yyyy-mm-dd" to Java. SQL. Date:


Simpledateformat bartdateformat = new simpledateformat ("yyyy-mm-dd ");  
String datestringtoparse = "2007-7-12 ";  
Try {  
Java. util. Date = bartdateformat. parse (datestringtoparse );  
Java. SQL. Date sqldate = new java. SQL. date (date. gettime ());
System. Out. println (sqldate. gettime ());  
}  
Catch (exception ex ){  
System. Out. println (ex. getmessage ());  
}


 



Java. util. Date, java. util. Calendar, java. SQL. Date, java. SQL. Time, java. SQL. timestamp difference and contact:



 



In Java-related project development, we often need Java. util. date, Java. util. calendar, Java. SQL. date, Java. SQL. time, Java. SQL. timestamp and other classes are used for date processing and analysis. What are the relationships between these functions and what are their differences?



First, let's talk about the relationship between them, Java. util. the date class is Java. SQL. date, Java. SQL. time, Java. slq. the parent class of the timestamp class. 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. gethour () method to access the hour information, this method will throwIllegalargumentexceptionException. 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 row of the database is only a field related to saving the date.



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 is used to obtain information about the Year. This method throwsIllegalargumentexceptionException. 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. TimestampThe field expands the java. util. Date class, which adds millisecond-level access control based on the java. util. Date class. Therefore, you can useGetnanos()Method to obtain the millisecond seconds of the time (note that the obtained time is in the unit of microseconds, 1 second equals to one billion microseconds), the same, this is also 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?



The Java. util. Calendar class 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, in the past year, the calendar Ar is the current year, for example, 2010. When the date is used to obtain the year, the current year-1900 is obtained (2010-1900 = 110). Therefore, the past value after you call getyear is 110 ). Last, calendar is an abstract class. It can be instantiated because the calendar acts as a factory-like function. In the getinstance method, the calendar subclass gregoriancalendar is instantiated and returned to the customer for use.



 


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.