There were some poorly designed aspects of the time library prior to Java8 that made it very inconvenient and error-prone to use. For example, to add a specified time to a specified date, you need a lot of boilerplate code. And its months start at zero, the slightest mistake will fall into the pit. Therefore, we usually use the third-party library Joda Time for time-dependent operations.

Use JodaTime

JodaTime’s home page on Github: JodaTime

There are two configurations when using JodaTime:

In the Maven:

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.9.9</version>
</dependency>
Copy the code

In the Gradle:

The compile 'joda - time: joda - time: 2.9.9'Copy the code

2. Get DateTime instance

When using JodaTime, you first get a DateTime instance and then string it together with other methods for powerful functionality. You can get a DateTime instance in a number of ways. Here are some common ways:

Method 1: Construct DateTime instances using system time

DateTime dateTime = new DateTime();
Copy the code

Approach 2: Construct DateTime instances with specific times, and there are many overloaded versions of this approach

DateTime dateTime1 = new DateTime(
2000, // year
1,    // month
1,    // day
0,    // hour (midnight is zero)
0,    // minute
0,    // second
0     // milliseconds
);
Copy the code

Method 3: Construct a DateTime instance using Calendar

DateTime dateTime2 = new DateTime(Calendar.getInstance());
Copy the code

Method 4: Construct DateTime instances using other DateTime instances

DateTime dateTime3 = new DateTime(dateTime);
Copy the code

Method 5: Construct DateTime instances using strings

DateTime dateTime4 = new DateTime("2006-01-26T13:30:00-06:00");
DateTime dateTime5 = new DateTime("2006-01-26");
Copy the code

3. Use DateTime methods

There are many methods in DateTime, and here we have divided the common methods into two categories. Those that return DateTime in a method, and those that return Property in a method. Obviously, to continue the concatenation operation, you need to call the instance method of Property.

Here, we start with the first class of methods in DateTime.

// Add the specified value to the specified time unit DateTime dateTime0 = datetime.plusdays (1); System.out.println(dateTime0); DateTime6 = datetime.minusdays (1); dateTime6 = datetime.minusdays (1); System.out.println(dateTime6); DateTime7 = datetime. withYear(2020); dateTime7 = datetime. withYear(2020); System.out.println(dateTime7); Println (datetime.toString ("E MM/dd/ YYYY HH: MM: ss.sss "));Copy the code

In the above code, we give examples of only a few of these methods. In fact, there are many methods inside 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 changes). In this method, if the number of milliseconds passed is found to be different from the current number of milliseconds, a new DateTime instance is created and returned. So, the DateTime returned by plusDays(1) and minusDays(1) above is actually another instance.

4, use Property

DateTime’s zipgex (), dayOfYear(), minuteOfDay(); You can then get another DateTime instance by calling the Property method. That is, the DateTime method is actually called to get the Property instance in order to modify the information at the specified time and location. For example, modify the “day”, modify the “year” and so on. After making the changes, you still need to get an instance of DateTime and continue with the subsequent operations.

In fact, every time a DateTime method is called to get a Property instance, the current DateTime is passed in as an argument. 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:

/ / here with dayOfMonth first obtain a Property instances, then call it withMaximumValue method / / it is the meaning of other date remains the same for a specified date, month after become the biggest returns a DateTime, that is, if the incoming is on May 1, 2018, Will return to May 31, 2018, // year, month, second, etc. unchanged, day becomes the month's largest. DateTime dateTime0 = dateTime.dayOfMonth().withMaximumValue(); DateTime dateTime1 = dateTime.dayOfMonth().withMinimumValue();Copy the code

5. Other static methods

In addition to the above classes, JodaTime has a number of static methods that we can use. Such as:

System.out.println(Days.daysBetween(dateTime1, dateTime).getDays());
System.out.println(Months.monthsBetween(dateTime1, dateTime).getMonths());
System.out.println(Years.yearsBetween(dateTime1, dateTime).getYears());
Copy the code

Of course, we only listed operations on “day,” “month,” and “year” units for two DateTime instances, and there are many similar classes for “millisecond,” “second,” and so on.

conclusion

Here are just a few examples of JodaTime’s common methods to illustrate the rationale behind its design. The emphasis is on sorting out the logic and understanding what each sequential operation does.

Code: java-advanced