How can I compare two Timestamp dates in Java?

In my Firebase database, for each item present, I save the date of when it is created and inserted into the db. The date is saved using the ServerValue.TIMESTAMP, the value is saved as a Map. How can I compare the saved value to today’s current date in Java?

I need to know if the date of the item is older than 14 days. THX

enter image description here


The number in your screenshot represents the number of milliseconds from the epoch which you can convert into an Instant using Instant.ofEpochMilli. Once you have Instant, you can convert it into other date-time types e.g.

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1616686018322L), ZoneOffset.UTC);
        ZonedDateTime now =;
        if (now.minusDays(14).isAfter(zdt)) {
            System.out.println("The item is 14 or more days older");
        } else {
            System.out.println("The item less than 14 days older");


The item less than 14 days older

Note that the comparison shown above takes all units (up to nanoseconds precision) into account. However, if you want to compare only dates (not date & time), you can perform the comparison on ZonedDateTime#toLocalDate.

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

Leave a Reply

Your email address will not be published. Required fields are marked *