In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail about the need for careful use of Date and Time classes in JAVA. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
The date and time classes provided in AVA, java.sql.Date and java.sql.Time, only read a certain part of the value from the database, which sometimes results in data loss. For example, a field that contains PM at 5:00:57 on 2002-05-22 will get 2002-05-22 when reading the date and 5:00:57 PM when reading the time.
You need to know the accuracy of the time stored in the database. Some databases, such as MySQL, have millisecond precision, while others, including Oracle, store data of type SQL DATE when millisecond data is not saved. BUG that is not easy to find is easy to occur in the following operations:
Get a date object in JAVA.
Read the date from the database
An attempt was made to compare whether two date objects are equal. If millisecond parts are lost, two date objects that were supposed to be equal may return false using the Equals method.
The java.sql.Timestamp class is more accurate than the java.util.Date class. This class contains a getTime () method, but it does not return extra precision data, so you must use the getNanos () method. The value of the nanosecond part (that is, the extra precision part) may be one millisecond larger than the value without this part. If you know that the database you are using holds the millisecond portion, you can use the following code to get the value:
Long time=timestamp.getTime () + timestamp.getNanos () / 1000000
The nanosecond part is why a java.sql.Timestamp object is not equal to a java.util.Date object, while a java.util.Date object may be equal to a java.sql.Timestamp object. This breaks the symmetry that the equals method should maintain.
It is important to use time-dependent classes in the java.sql package, but they can also lead to human error.
This is because although the complexity of these classes is clearly described in javadoc, few people read these documents because they look simple.
This is the end of this article on "careful use of Date and Time classes in JAVA". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.