In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
When it comes to the time stamp in java, I believe many people have used it.
It's not just the java.sql.Timestamp class, two constructors, and 13 methods, which is probably the simplest basic class in java.
As the saying goes, those who drown can swim, and the ones that make us stumble in the development are often some familiar libraries.
Introduction
First, let's look at the output of the following lines of code.
Timestamp T1 = new Timestamp (0); System.out.println (T1); Timestamp T2 = Timestamp.valueOf ("2037-12-31 23:59:59"); System.out.println (t2.getTime ()); System.out.println (t2.getTime () = = new Long (Integer.MAX_VALUE) * 1000); Timestamp T3 = new Timestamp (new Long (Integer.MAX_VALUE) * 1000); System.out.println (T3); Timestamp T2 = Timestamp.valueOf ("9999-12-31 23:59:59") System.out.println (t21.getTime ()); Timestamp T22 = new Timestamp (t21.getTime ()); System.out.println (T22); Date D1 = new Date (0); System.out.println (d1.equals (T1)); System.out.println (t1.equals (D1))
If you can clearly say what each output is and why, then you don't need to read on.
If you are confused about one or more results, let's step on the pit together.
Time range
There are many ways to figure out the scope of the Timestamp class, you can read the documentation, look at the source code, write a code test, or search engines to see what others say.
When we have practiced one or more of the above methods, we will look back at the output of the first and second paragraphs of code in our introduction.
Timestamp T1 = new Timestamp (0); System.out.println (T1); / / 1970-01-01-01 08 System.out.println 0000.0
Does it look familiar? It is only 8 hours short of Greenwich mean time (UTC is more commonly used by computers), so it is easy to understand that we are East eighth District.
In other words, the start time of the Timestamp class can be thought of as Greenwich mean time, and the offset of the time zone is added to the use of different time zones.
Timestamp T2 = Timestamp.valueOf ("2037-12-31 23:59:59"); System.out.println (t2.getTime ()); / / 2145887999000System.out.println (t2.getTime () = = new Long (Integer.MAX_VALUE) * 1000); / / falseTimestamp T3 = new Timestamp (new Long (Integer.MAX_VALUE) * 1000); System.out.println (T3); / 2038-01-19 11:14:07.0Timestamp T21 = Timestamp.valueOf ("9999-12-31 23:59:59") System.out.println (t21.getTime ()); / / 253402271999000Timestamp T22 = new Timestamp (t21.getTime ()); System.out.println (T22); / / 9999-12-31 23 253402271999000Timestamp 599.0
The above pieces of code are actually validated based on the maximum value of Timestamp on the Internet.
The first kind is 2037-12-31 23:59:59 (T2)
It can be found that the difference between the number of milliseconds obtained and Integer.MAX_VALUE is about 1000 times.
It can be inferred that this statement is based on the number of milliseconds stored in a 32-bit system, the maximum at the end of the month or the end of the year without overflow.
The next time constructed by Integer.MAX_VALUE (T3) is 2038-01-19 11-14-7. 0 also validates our inference.
The second kind is 9999-12-31 23:59:59
This time is the maximum time we have in the current time format, and it is found that the Timestamp class can store this kind of time through the verification of t21recoveryt22.
Looking back at the source code, it was found that the Long used to store milliseconds instead of Integer,64 bits could have lasted until 9999.
Through the above verification, we can confirm that the maximum time of the Timestamp class can be 9999-12-31 23:59:59.
Tips:
In actual use, our data needs to be stored in the database.
Take mysql as an example. If you take it for granted that the Timestamp of java corresponds directly to the Timestamp of the database, you will find that the ranges of the two data types are different.
An excerpt from an official mysql document:
The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is' 1000-01-01'to '9999-12-31. The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is' 1000-01-01'to '9999-12-31.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is' 1000-01-01 00 to '9999-12-31 23 59.
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00 UTC 01' UTC to' 2038-01-1903 UTC.
In the real use of the business, java class library, database data types for comparison, as far as possible to find the combination that meets the requirements.
Equals is not equal.
It takes time, of course, we have to compare it. Take a look at the output of the following code.
Date D1 = new Date (0); System.out.println (d1.equals (T1)); / / trueSystem.out.println (t1.equals (D1)); / / false
Very interesting result. Let's take a look at how he did it. Look at the source code > >
Date.equals:
Public boolean equals (Object obj) {return obj instanceof Date & & getTime () = ((Date) obj) .getTime ();}
Because Timestamp inherits from Date, and the loss of high-precision parts after a strong turn causes getTime to be completely consistent, the first comparison returns true
Timestamp.equals:
Public boolean equals (java.lang.Object ts) {if (ts instanceof Timestamp) {return this.equals ((Timestamp) ts);} else {return false;}}
Determine that Date is not of Timestamp type, and return false directly.
There's nothing wrong with the code, but going back to the real world means a = b and b! = a, doesn't it always feel uncomfortable to be a serious programmer?
I believe the same is true of the author. First, this explanation appears on the class.
The inheritance relationship between Timestamp and java.util.Date really denotes implementation inheritance, and not type inheritance.
And then the method needs to be made up.
Note: This method is not symmetric with respect to the equals (Object) method in the base class.
The main idea is: I write this inheritance relationship only to implement inheritance, not type inheritance (in order to write less code, it doesn't matter), the equals method is not equivalent to the equals method of the parent class (I'm not responsible for your misuse)
Summary
Finally, the following points are summarized.
1. The acceptable time range for java.sql.Timestamp is 1970-01-01 00:00:00-9999-12-31 23:59:59. The offset caused by the time zone should be taken into account when using it.
2. When used in conjunction with the database, you need to select the appropriate scope according to the actual needs and the specific scope of the database data type, not just by name.
3. The equals method of the Timestamp class is not equivalent to the equals method of the parent class, and the consistent result can be returned only if the two compared objects are Timestamp.
4, we try our best to avoid this uncomfortable design in actual design and development, and combine inheritance, encapsulation, polymorphism and other means to ensure the robustness of our design.
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.