Java Date Comparison and Sorting: Common methods and techniques of Date class
Java Date Comparison and Sorting: Common methods and techniques of Date class
In Java programming, comparison and sorting the date are very common operations.Java provides a rich date and time processing library, where the Date class is used to represent the basic category of the date and time.This article will introduce common methods and techniques of Date class, as well as how to use them for date comparison and sorting.
1. Basic usage of the date class
The Date class is a class in the java.util package, which represents a specific time point.To create a DATE object, you can use the constructor without constructing, which will return the current date and time.
For example:
Date currentDate = new Date();
The Date class provides many methods to obtain each part of the date and time, such as the year, month, day, hours, minutes and seconds.The following is some commonly used methods in the date class:
1. Gettime () -The return from January 1, 1970 to the milliseconds representing the current Date object.
2. Getyear () -ee the year of the current DATE objects subtracted 1900.
3. Getmonth () -En the range of the current DATE object, the range is 0 to 11, of which 0 means January.
4. Getdate () -Enate the date of the current DATE object (the number of a month).
5. Gethours () -ee the number of hours of the current Date object (0-23).
6. Getminutes () -ee the number of minutes to represent the current Date object (0-59).
7. Getseconds () -whind the number of seconds represented by the current Date object (0-59).
Using these methods, you can get each part of a certain time when you need.
2. Date comparison and sorting
In practical applications, we often need to compare and sort the date.The Date class provides the compareto () method to compare the date.This method uses the two Date objects as a parameter and returns an integer value to indicate the relationship between them.
For example:
Date date1 = new Date();
Date date2 = new Date();
int result = date1.compareTo(date2);
The meaning of returning value Result is as follows:
-If Date1 returns a negative number before date2.
-If Date1 returns a positive number after date2.
-If the same day and date2, return 0.
Based on this return value, you can compare the date to achieve the sorting operation of the date.
For example, if you have a date array, you can use the sort () method in the Arrays class to sort the date, as shown below:
import java.util.Date;
import java.util.Arrays;
public class DateSortingExample {
public static void main(String[] args) {
Date[] dates = new Date[5];
dates[0] = new Date();
dates[1] = new Date(System.currentTimeMillis() - 1000000000);
dates[2] = new Date(System.currentTimeMillis() + 1000000000);
dates[3] = new Date(System.currentTimeMillis() - 2000000000);
dates[4] = new Date(System.currentTimeMillis() + 2000000000);
Arrays.sort(dates);
// Print the date after sorting
for (Date date : dates) {
System.out.println(date);
}
}
}
Through this example, we can see that the sorted date array is sorted in the order of time.
3. Summary
This article introduces the basic usage of the DATE class in the Java and how to compare the date and sort.We have learned the commonly used methods in the Date class, such as each part of the date and time.At the same time, according to the return value of the compareto () method, we can achieve comparison and sorting the date.I hope this article will provide you with some help and guidance during the date related operation.
The above is the content of the Java date comparison and sorting. I hope it will be helpful to you.Please enjoy the fun of Java programming!