Millisecond Date Comparison

Let's say I have two date fields: getDate and currentDate. I want to check if a getDate message was received 5 days before currentDate. I did to convert dates in milliseconds and then compare with 5. Is there a better way to do this? If so, how and why is my less better? Thank.

The method I wrote is

private static final double DAY_IN_MILLISECONDS = 86400000;

// Param date is the receivedDate
private long getDaysOld(final Date date) {


    Calendar suppliedDate = Calendar.getInstance();
    suppliedDate.setTime(date);
    Calendar today = Calendar.getInstance();
    today.setTime(currentDate);

    double ageInMillis = (today.getTimeInMillis() - suppliedDate.getTimeInMillis());
    double tempDouble;

    if(isEqual(ageInMillis, 0.00) || isGreaterThan(Math.abs(ageInMillis), DAY_IN_MILLISECONDS)) {
        tempDouble =  ageInMillis / DAY_IN_MILLISECONDS;
    } else {
        tempDouble =  DAY_IN_MILLISECONDS / ageInMillis;
    }

    long ageInDays = Math.round(tempDouble);

    return ageInDays;


}

Then I have something like -

long daysOld = getDaysOld(receivedDate) ;   
if(daysOld <= 5) {
    .... some business code ....
}
+3
source share
6 answers

It can be shortened:

int daysOld = (System.currentTimeMillis() - date.getTime()) / DAY_IN_MILLISECONDS;
+1
source

try joda-time . Timing with your own API is always better anyway. Time in Joda makes this type of MUUUCH calculation easier and will also process time zones.

+2
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class Test {

    private static long DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;

    public static void main(String[] args) throws Exception {
        //
        Date currentDate = getGregorianDate(1990, Calendar.JANUARY, 20);
        Date receiveDate = getGregorianDate(1990, Calendar.JANUARY, 23);
        //
        if (getDifferenceBetweenDates(receiveDate, currentDate) < 5 * DAY_IN_MILLISECONDS) {
            System.out.println("Receive date is not so old.");
        }
        else {
            System.out.println("Receive date is very old.");
        }
    }

    private static long getDifferenceBetweenDates(Date date1, Date date2) {
        return Math.abs(date1.getTime() - date2.getTime());
    }

    private static Date getGregorianDate(int year, int month, int date) {
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.set(year, month, date);
        return calendar.getTime();
    }

}
+2

24 * 60 * 60 * 1000, - ( 23 25 ).

, 28/03/2010. 27/03/2010 28/03/2010 1 , , 0.

:

public static long daysBetween(Date dateEarly, Date dateLater) {
    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(dateEarly);  
    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(dateLater);

    long endL = cal2.getTimeInMillis() + cal2.getTimeZone().getOffset( cal2.getTimeInMillis() );
    long startL = cal1.getTimeInMillis() + cal1.getTimeZone().getOffset( cal1.getTimeInMillis() );
    return (endL - startL) / (24 * 60 * 60 * 1000);
}

public static void main(String[] args) throws Exception {

    TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));  

    Date foo = new Date(2010,02,27);
    Date bar= new Date(2010,02,28);

    System.out.println(daysBetween(foo,bar)); //prints 1
}
0

, " ". - , ​​ ? , , , . , ; , . , : , , , , . - , , ( !) .

So, I think that you want to compare only dates, not time. You can do this using the Calendar, resetting all time fields. Given the arrived date and locale (as you can tell when midnight), I think this is correct:

    Calendar deadline = Calendar.getInstance(locale);
    deadline.set(Calendar.HOUR_OF_DAY, 0);
    deadline.set(Calendar.MINUTE, 0);
    deadline.set(Calendar.SECOND, 0);
    deadline.set(Calendar.MILLISECOND, 0);
    deadline.add(Calendar.DAY_OF_MONTH, 5);

    Calendar arrived = Calendar.getInstance(locale);
    arrived.setTime(arrivedDate);
    deadline.set(Calendar.HOUR_OF_DAY, 0);
    deadline.set(Calendar.MINUTE, 0);
    deadline.set(Calendar.SECOND, 0);
    deadline.set(Calendar.MILLISECOND, 0);

    boolean arrivedWithinDeadline = arrived.compareTo(deadline) <= 0;

You must check this carefully before using it.

0
source

Below is my method, which returns me the exact difference in days,

/**
 * method to get difference of days between current date and user selected date
 * @param selectedDateTime: your date n time
 * @param isLocalTimeStamp: defines whether the timestamp d is in local or UTC format
 * @return days
 */
public static long getDateDiff(long selectedDateTime, boolean isLocalTimeStamp)
{
    long timeOne = Calendar.getInstance().getTime().getTime();
    long timeTwo = selectedDateTime;
    if(!isLocalTimeStamp) 
        timeTwo += getLocalToUtcDelta();
    long delta = (timeOne - timeTwo) / ONE_DAY;

    if(delta == 0 || delta == 1) {
        Calendar cal1 = new GregorianCalendar();
        cal1.setTimeInMillis(timeOne);
        Calendar cal2 = new GregorianCalendar();
        cal2.setTimeInMillis(timeTwo);
        long dayDiff = cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);
        return dayDiff;
    }

    return delta;
}
0
source

Source: https://habr.com/ru/post/1771540/


All Articles