[Java] How to set the Date time to 00:00:00

Overview

--I want to find out if the date to be checked is before, after, or on the day of today. --However, the date to be checked is 00:00:00, so I want to compare only the dates. --Today's date generated by new Date () is up to the time --By comparison, even if the dates are the same, if the times are different, it will not be judged on the day.

Realization method

Method to use

--Compare before and after using the method of java.util.Date --Use the well-known DateUtils from ʻorg.apache.commons.lang3`

UseMethod


java.util.Date.equals(Object obj)
java.util.Date.before(Date when)
java.util.Date.after(Date when)

org.apache.commons.lang3.time.DateUtils.truncate(Date date, int field)

sample

DateUtilService.java


public void printDate(Date targetDate) {

    //00 today:00:Get 00
    Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);

    if (targetDate.equals(today)) {
        System.out.println("It's today.");
    } else if (targetDate.after(today)) {
        System.out.println("It is a date after today.");
    } else if (targetDate.before(today)) {
        System.out.println("It is a date before today.");
    }
}

Commentary

  1. Enter the date you want to pad with zeros in the first argument of DateUtils.truncate
  2. Pass Calendar.DAY_OF_MONTH as the second argument
  3. Today's time will be 00:00:00, so the format will be the same as targetDate.
  4. After that, check the context with equals, after, before

By the way

How to use DateUtils.isSameDay

If it's just the day or not, you can use DateUtils.isSameDay to compare only the dates and return a boolean without truncate at 00:00:00.

When changing the position to be zero

You can change the position to zero depending on the variable passed to the second argument of DateUtils.truncate.

int field Position to be zero
Calendar.DAY_OF_MONTH 00:00:00
Calendar.HOUR_OF_DAY XX:00:00
Calendar.MONTH 00 a day:00:00
Calendar.YEAR 1/1 00:00:00

reference

[Java] How to truncate the hour, minute, and second of Date Class DateUtils|org.apache.commons.lang3.time

Recommended Posts

[Java] How to set the Date time to 00:00:00
How to get the date in java
[Java] (for MacOS) How to set the classpath
How to set Java constants
[Java] How to get the current date and time and specify the display format
How to set chrony when the time shifts in CentOS7
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
[Java] How to use the File class
Introduction to java for the first time # 2
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to measure program execution time
[Java] How to get the current directory
Java 8 to start now ~ Date time API ~
[Processing × Java] How to use the class
How to install the legacy version [Java]
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] Date / time operations
[Java] How to use Thread.sleep to pause the program
[Java] Set the time from the browser with jsoup
How to use the replace () method (Java Silver)
The date time of java8 has been updated
[Java] How to get the redirected final URL
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
Spring Boot --How to set session timeout time
[Java] How to use Calendar class and Date class
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
[Java] How to use Map
[Java] How to omit the private constructor in Lombok
How to lower java version
[Java] How to use Map
[Java] I want to calculate the difference from the date
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to write Scala from the perspective of Java
How to use java Optional
[Java] How to extract the file name from the path
How to minimize Java images
How to write java comments
[Java] How to use Optional ②
[Android Studio] [Java] How to fix the screen vertically
[Java] How to use removeAll ()
How to set Docker nginx
[Java] How to use string.format
How to use Java Map
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
Input to the Java console
Summary of how to use the proxy set in IE when connecting with Java
Difference between Java and JavaScript (how to find the average)