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/03 Report--
In this issue, the editor will bring you about how to understand the date and time API and JSR310 in Java 8. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The blog has not been updated for a month, this time I will tell you about the java8 time and date API.
As we all know, date is a key part of business logic computing, and any enterprise application needs to deal with the issue of time. Applications need to know the current time point and the next time point, and sometimes they have to calculate the path between these two time points. But the date before java is disgusting. Let's complain first.
Complaining about java.util.Date and Calendar
Tiago Fernandez voted once, voting for the worst JAVA API, the EJB2.X, and the second date API.
Slot point one
At the beginning, Date not only had to carry date information, but also to convert dates, but also to display different date formats.
Since then, starting with JDK 1.1, these three responsibilities have been separated:
Use the Calendar class to convert between date and time fields
Use the DateFormat class to format and parse date strings
Date is only used to carry date and time information.
The corresponding method in the original Date is obsolete. However, both Date and Calendar are too inconvenient to use, which is where API is not designed.
Slot point two
Stupid year and month.
Date date = new Date (2012 System.out.println 1); System.out.println (date); output Thu Feb 01 00:00:00 CST 3912
Observe the output result, year is 2012-1900, and month, didn't I give 1 for the month parameter? How to output February (Feb)?
Someone should have told you that if you want to set a date, you should use java.util.Calendar, like this.
Calendar calendar = Calendar.getInstance (); calendar.set (2013, 8, 2)
It's not right to write it this way. Calendar's month also starts with 0, indicating that the number 7 should be used in August, or simply enumerate.
Calendar.set (2013, Calendar.AUGUST, 2)
Notice the code above that the value of the Calendar year does not need to be subtracted by 1900 (of course, the definition of the month is the same as the Date). This inconsistency is maddening!
Some people may know that the Calendar-related API was donated by IBM, which led to inconsistencies.
Slot point three
All properties in java.util.Date and java.util.Calendar are mutable
The following code calculates the number of days between two dates.
Public static void main (String [] args) {Calendar birth = Calendar.getInstance (); birth.set (1975, Calendar.MAY, 26); Calendar now = Calendar.getInstance (); System.out.println (daysBetween (birth, now)); System.out.println (daysBetween (birth, now)); / / display 0? } public static long daysBetween (Calendar begin, Calendar end) {long daysBetween = 0; while (begin.before (end)) {begin.add (Calendar.DAY_OF_MONTH, 1); daysBetween++;} return daysBetween;}
There is a problem with daysBetween. If you calculate two Date instances in succession, you will get 0 the second time, because the state of Calendar is variable. Considering the situation of repeated calculation, * copy a new Calendar.
Public static long daysBetween (Calendar begin, Calendar end) {Calendar calendar = (Calendar) begin.clone (); / / copy long daysBetween = 0; while (calendar.before (end)) {calendar.add (Calendar.DAY_OF_MONTH, 1); daysBetween++;} return daysBetween;} JSR310
All these have led to the birth of some third-party java date libraries, such as the widely used JODA-TIME, and Date4j, etc. Although the third-party library is powerful and easy to use, there are still compatibility problems, such as the standard JSF date converter and joda-time API are not compatible, you need to write your own converter, so the standard API is still necessary, so there is JSR310.
JSR 310 actually has two concepts of dates. * Instant, which roughly corresponds to the java.util.Date class, because it represents a definite point in time, that is, the offset from the standard Java era (January 1, 1970); but unlike the java.util.Date class, it is accurate to the nanosecond level.
The second corresponds to the ideas of human beings, such as LocalDate and LocalTime. They represent the general concept of a time zone, either date (excluding time) or time (excluding date), similar to the way java.sql represents it. In addition, there is a MonthDay that stores someone's birthday (not including the year). Each class stores the correct data internally rather than using 12:00 at midnight to distinguish the date and 1970-01-01 to represent the time.
At present, Java8 has implemented all the contents of JSR310. A new class defined by the java.time package represents the rules of the date-time concept, including instants, durations, dates, times, time-zones and periods. These are all based on the ISO calendar system, which follows the Gregorian rules. The most important point is that the values are immutable and thread-safe. Through the following figure, we can quickly take a look at the format of the values of some of the major classes under the java.time package for easy understanding.
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.