Common date operations in Java (value taking, conversion, addition and subtraction, comparison)

During the development of Java, it is inevitable to get entangled with the date type. We are ready to summarize the date related operations often used in the project. JDK version 1.7 can help you save a few minutes, get up and make a cup of coffee  It's excellent, hehe. Of course, I only provide feasible solutions, which are not guaranteed to be best practices. Welcome to discuss.

1. Date value

stay  In the era of the old JDK, many code used java util. Date class, but because date class is not convenient for internationalization, it is actually from jdk1 1, Java is more recommended util. The calendar class handles time and date. The operation of the date class will not be introduced here. Let's go straight to the topic of how to use the calendar class to obtain the current date and time.

Since the constructor method of calendar is modified by protected, we will create the calendar object through the getInstance method provided in the API.

Then we can get the current time parameters through this object.

To get other types of time data, just modify now In addition to the above three parameters, other common parameters in get() are as follows:

•Calendar. DAY_ OF_ Month: date, and calendar Same as date • calendar Hour: hours in 12 hour system • calendar HOUR_ OF_ Day: the number of hours in a 24-hour system • calendar Minute: minute • calendar Second: second • calendar DAY_ OF_ Week: day of week

In addition to obtaining time data, we can also set various time parameters through the calendar object.

be careful:

• when the time parameter is set, other relevant values will be recalculated. For example, when you set the date to the 11th, the day of the week will change accordingly. • The month obtained plus 1 is the actual month. • In the calendar class, Sunday is 1, Monday is 2, and so on.

2. Date conversion

After talking about date values, let's talk about date conversion. Conversion is generally the mutual conversion between date type date and string type string. I mainly use Java text. Simpledateformat for conversion.

be careful:

• the conversion format must be specified when creating a simpledateformat object.

• the conversion format is case sensitive. Yyyy represents the year, mm represents the month, DD represents the date, HH represents the hour in base 24, HH represents the hour in base 12, mm represents the minute and SS represents the second.

3. Date addition and subtraction

Generally speaking, we add or subtract dates in two ways:

• based on a date, calculate the date a few days ago / after, a few years ago / after, or before and after other time units

Note that the add method of the calendar object is used to change the calendar Year is any time unit field,  Complete date calculation under various time units.

• calculate the interval between two times, for example, calculate the number of days from January 1, 2016 to now.

4. Date comparison

Looking at my previous code, I found that whenever I perform date comparison, I always convert the date to a string in the format of "yyyymmdd", then convert the string to a numeric value, and then compare the size of the numeric value. Ha ha, a simple comparison operation, but I have to write more than ten lines of code, which is a little unbearable. Now let's talk about the correct date comparison posture.

There are generally two methods for comparing dates. For Java util. Date or Java util. Calendars are universal. One is through the after () and before () methods, and the other is through the CompareTo () method.

The above is the full description of common date operations (value taking, conversion, addition and subtraction, comparison) in Java introduced by this article. I hope you like it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>