In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the specific use of Java third-party library JodaTime, the content is very detailed, interested friends can refer to, hope to be helpful to you.
There are some poorly designed areas in the time library before Java8, which makes it very inconvenient to use and error-prone. For example, to add a specified time to a specified date, you need a lot of boilerplate code; and its month starts at 0, and it will fall into the pit at the slightest inadvertent. Therefore, we usually use the third-party library Joda Time for time-related operations.
1. Use JodaTime
JodaTime's home page on Github: JodaTime
There are two configuration methods when using JodaTime:
In Maven:
Joda-time joda-time 2.9.9
In Gradle:
Compile 'joda-time:joda-time:2.9.9'
Core class
The five most frequently used date-time classes:
-Instant-an immutable class used to represent an instantaneous point on the timeline (timestamp)
-DateTime-an immutable class that replaces JDK's Calendar class
LocalDate-an immutable class that represents a local date without the time part (no time zone information)
-LocalTime-an immutable class that represents a local time without the date part (no time zone information)
-LocalDateTime-an immutable class that represents a local date-time (no time zone information)
2. Obtain DateTime instance
When using JodaTime, you first need to get an instance of DateTime, and then use its other methods to concatenate to achieve powerful functions. There are many ways to get an instance of DateTime. Here are a few common ways:
Method 1: use system time to construct DateTime instances
DateTime dateTime = new DateTime ()
Method 2: construct a DateTime instance with a specific time, which has many overloaded versions
DateTime dateTime1 = new DateTime (2000, / year1, / / month1, / / day0, / / hour (midnight is zero) 0, / / minute0, / / second0 / / milliseconds)
Method 3: use Calendar to construct DateTime instances
DateTime dateTime2 = new DateTime (Calendar.getInstance ())
Method 4: use other DateTime instances to construct DateTime instances
DateTime dateTime3 = new DateTime (dateTime)
Method 5: use strings to construct DateTime instances
DateTime dateTime4 = new DateTime ("2006-01-26T13:30:00-06:00"); DateTime dateTime5 = new DateTime ("2006-01-26"); 3. Methods of using DateTime
There are many methods in DateTime, and here we divide the commonly used methods into two categories. One is the kind that returns DateTime in the method, and the other is the kind that returns the Property type in the method. Obviously, if the latter kind of concatenation operation continues, you need to call the instance method of Property.
Here, we first give the first kind of method in DateTime.
/ / increase the specified value DateTime dateTime0 = dateTime.plusDays (1); System.out.println (dateTime0) above the specified time unit; / / decrease the specified value DateTime dateTime6 = dateTime.minusDays (1); System.out.println (dateTime6) above the specified time unit; / / in addition to the date of increase or decrease, you can also directly specify the value above its specified time unit DateTime dateTime7 = dateTime.withYear (2020); System.out.println (dateTime7) / / output date System.out.println (dateTime.toString ("E MM/dd/yyyy HH:mm:ss.SSS")) in the specified format
In the above code, we only give examples of some of these methods. In fact, there are many methods within DateTime, but their principles are basically similar.
Some of the above methods call the withMillis () method of the DateTime instance if the time involved changes (specifically, the number of milliseconds corresponding to the time). In this method, if it is found that the number of milliseconds passed in is different from the current number of milliseconds, a new instance of DateTime is created and returned. So the DateTime returned by plusDays (1) and minusDays (1) above is actually another instance.
4. Using Property
A Property instance of the DateTime instance can be obtained through some column methods such as millisOfDay () dayOfYear () minuteOfDay () of the DateTime instance, and then another DateTime instance can be obtained by calling the Property method. That is, actually calling the method of DateTime to get the Property instance is to modify the information of the specified time and location. For example, the revision of "day", the revision of "year" and so on. After the modification, you still need to get an instance of DateTime before continuing with the subsequent operation.
In fact, every time you call the method of DateTime to get the Property instance, you pass in the current DateTime as a parameter. Then when the specified method is called, the withMillis () method of the DateTime instance is called to determine whether the time has changed, and if so, a new instance is created and returned.
Here are some examples of it:
/ / first use dayOfMonth to obtain a Property instance, and then call its withMaximumValue method / / it means that the other dates of the specified date remain unchanged, and a DateTime is returned when the month becomes the largest, that is, if the input is May 1, 2018, the positions such as May 31, / / year, month, and seconds remain unchanged, and the day becomes the largest for that month. DateTime dateTime0 = dateTime.dayOfMonth (). WithMaximumValue (); DateTime dateTime1 = dateTime.dayOfMonth (). WithMinimumValue (); 5. Other static methods
In addition to some of the classes above, JodaTime has a number of static methods for us to use. For example:
System.out.println (Days.daysBetween (dateTime1, dateTime). GetDays (); System.out.println (Months.monthsBetween (dateTime1, dateTime). GetMonths (); System.out.println (Years.yearsBetween (dateTime1, dateTime). GetYears ())
Of course, we only list the operations on the "day", "month" and "year" units of the two DateTime instances, and there are many similar classes that can be used to operate on "milliseconds", "seconds" and so on.
On the specific use of Java third-party library JodaTime is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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.