1

Helo everyone!

I'm trying to compare two Dates in java, to know if one is before the actual day, but it compares the time too, so regardless is the same day, it says its before becouse compares them with the time. Is there any way to just know if the date is before the current day, not the time?

Here is the code 'im using (the date comes from a times

long timestampDate = 1623307684;
Date date = new Date((long)timestampDate*1000);
if(date.before(Calendar.getInstance().getTime()))
{
    System.out.println("Was yesterday");
}
else
{
    System.out.println("Is today");
}

Thank for your time!

Toast Bread
  • 19
  • 1
  • 4
  • 1
    You can use `LocalDate` class. – Héctor Jun 10 '21 at 09:54
  • 2
    Does this answer your question? [How to compare two Dates without the time portion?](https://stackoverflow.com/questions/1439779/how-to-compare-two-dates-without-the-time-portion) – Beshambher Chaukhwan Jun 10 '21 at 09:57
  • 1
    Reopened it as the duplicate target was not an exact match. This question differs from the duplicate target for two things: (1) It requires converting the epoch seconds to `Instant`. (2) The duplicate target talks only about the before/after scenario whereas this question also requires finding whether the timestamp refers to ***Yesterday***. – Arvind Kumar Avinash Jun 10 '21 at 11:05
  • I recommend you don’t use `Date` and `Calendar`. Those classes are poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 10 '21 at 19:41

1 Answers1

4

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*, released in March 2014.

Solution using java.time, the modern Date-Time API:

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        long timestampDate = 1623307684L;
        Instant instant = Instant.ofEpochSecond(timestampDate);

        // Replace JVM's timezone, ZoneId.systemDefault() with the applicable timezone
        // e.g. ZoneId.of("Etc/UTC")
        LocalDate date = instant.atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate today = LocalDate.now(ZoneId.systemDefault());
        if (date.isBefore(today)) {
            System.out.println("The given timestamp is for a date in the past.");
            // ...
        }

        if (date.equals(today.minusDays(1))) {
            System.out.println("Yesterday");
        }
    }
}

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Hi Arvind Kumar Avinash, thank you for your answer, i'm trying using this timestamp 1623308351, that is today, but earlier, and still saying is yesterday, And in response to your last edit, i don't only need if de date is yesterday, i need to know if is before the actual date. To provide context for this, is to check if the current login on a app is the first one of the day or not. – Toast Bread Jun 10 '21 at 10:04
  • 1
    @ToastBread - My code has both scenarios: (1) Whether the timestamp refers to a date in the past (2) Whether the timestamp refers to ***Yesterday***. The code does not print ***Yesterday*** as the timestamp is for today's date. Check the output [online](https://ideone.com/cNAC9y). The API is rich with many options. Let me know if you need any further help. – Arvind Kumar Avinash Jun 10 '21 at 10:14