Check two Java Date is not the same day N postures

I. Background

This article describes the comparison of the two java.util.Date objects are not representative of the N kinds of gestures on the same day.  

First introduced Java native API, and will introduce several libraries implement this function.

Translator: Comparison of two objects on the same day itself is not very difficult, focusing remind everyone to learn more about other libraries used, the functional requirements related to the future more time can be processed more easily through the relevant library.

 

Two, Core Java

Date class representing a particular instant in time, with millisecond accuracy.

In order to find out whether two Date object contains the same day, we need to check two objects Year-Month-Day are the same, discarding more granular time.

2.1. Use  LocalDate

Using the Java new Date-Time API 8, we can use LocalDate object. This object is immutable, no date indicates the target time.

Let's look at how you can use these to check whether two Date objects on the same day:

public static boolean isSameDay(Date date1, Date date2) {
    LocalDate localDate1 = date1.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
    LocalDate localDate2 = date2.toInstant()
      .atZone(ZoneId.systemDefault())
      .toLocalDate();
    return localDate1.isEqual(localDate2);
}

In this example, we will use the default time zone two Date objects are converted to LocalDate. After conversion, we only need to use the method to check whether isEqual LocalDate objects for equality.

Thus, using this approach, we will be able to determine whether two Date objects for the same day.

2.2. Use SimpleDateFormat

From the early versions of Java beginning, we have been able to use the SimpleDateFormat class Date and String object represents a transition between forms. Such incidental use multiple modes of conversion.

In our example, we will use the mode "yyyyMMdd".

Using this method, we will format dates, convert it to a String object, then use the standard equals method to compare:

public static boolean isSameDay(Date date1, Date date2) {
    SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
    return fmt.format(date1).equals(fmt.format(date2));
}

2.3 Using Calendar

Calendar class provides a method of obtaining a different value of the time units of the date and time.

First, we need to create two instances of Calendar, Calendar object and set the time according to each date.

Then, we can query separately and compare Year-Month-Day property to determine whether the object has the same date Date:

public static boolean isSameDay(Date date1, Date date2) {
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(date1);
    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(date2);
    return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)
      && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
      && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH);
}

3. External Library

By the above example, we passed the old and the new Java API comparison of two Java object is a date on the same day.

Here we will use the tripartite library to achieve the same functionality.

3.1 Using Apache Commons Lang package  DateUtils

DateUtils class provides many useful class time, use it to deal with Calendar and Date objects very easily.

maven dependence:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

Get a word:

DateUtils.isSameDay(date1, date2);

 

3.2. Joda-Time library

Joda-Time  provides a powerful date and time functions, you can replace Java date and time related classes.

maven dependence

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10</version>
</dependency>

usage:

public static boolean isSameDay(Date date1, Date date2) {
    org.joda.time.LocalDate localDate1 = new org.joda.time.LocalDate(date1);
    org.joda.time.LocalDate localDate2 = new org.joda.time.LocalDate(date2);
    return localDate1.equals(localDate2);
}

3.3. Date4J library

Date4J  also provides an API a lot of time-related.

maven dependence

<dependency>
    <groupId>com.darwinsys</groupId>
    <artifactId>hirondelle-date4j</artifactId>
    <version>1.5.1</version>
</dependency>

We can  java.util.Date into DateTime object and then use isSameDayAs to compare:

public static boolean isSameDay(Date date1, Date date2) {
    DateTime dateObject1 = DateTime.forInstant(date1.getTime(), TimeZone.getDefault());
    DateTime dateObject2 = DateTime.forInstant(date2.getTime(), TimeZone.getDefault());
    return dateObject1.isSameDayAs(dateObject2);
}

 

4 Summary

This article describes whether Java Date objects to compare two different ways on the same day.

I hope you understand the common tools of the time, using the well-known time of the tripartite tools in case with similar needs to simplify the code.

 

 

Published 379 original articles · won praise 862 · Views 1.32 million +

Guess you like

Origin blog.csdn.net/w605283073/article/details/103335373