简体   繁体   中英

Compare System.currentTimeMillis()

I want to compare two different time stamp using System.currentTimeMillis() .

Basically I want to check whether the time stamps are within 3 hours range on a particular day.

How to do this?

Considering you have time1 and time2 , where time1 <= time2 you can do this:

if (time1 >= time2-(60*60*3*1000)) {
   // interval is up to 3 hours
} else {
   // interval is more than 3 hours
}

Use Instant to get the time in the epoch and convert it to LocalDateTime to get the information about the day and check, if the first time plus 3 hours is smaller than the second time:

long millis1 = System.currentTimeMillis();
...
long millis2 = System.currentTimeMillis();

Instant instant1 = Instant.EPOCH.plusMillis(millis1);
Instant instant2 = Instant.EPOCH.plusMillis(millis2);

LocalDateTime t1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());
LocalDateTime t2 = LocalDateTime.ofInstant(instant2, ZoneId.systemDefault());

System.out.println("same day: " + (t1.getDayOfYear() == t2.getDayOfYear()));
System.out.println("t1+3h >= t2: " + (t1.plusHours(3).compareTo(t2) >= 0));

Here is a sample program that might help you out. The crux here is the method "findIfDatesWithinThreeHours" which helps whether to find if two instances are three hours apart.

package com.inrvu.adapter;

import java.util.Calendar;
import java.util.Date;


public class Tester {

public static void main(String[] args) {
    Date[] dates = getTwoDatesThreeHoursApart();
    System.out.println(String.format("%b", findIfDatesWithinThreeHours(dates[0],dates[1])));

    dates = getTwoDatesWithinThreeHours();
    System.out.println(String.format("%b", findIfDatesWithinThreeHours(dates[0],dates[1])));
}

public static Date[] getTwoDatesThreeHoursApart()
{
    Calendar calendar = Calendar.getInstance();
    Date date1 = calendar.getTime();
    calendar.add(Calendar.HOUR,4);
    Date date2 = calendar.getTime();
    return new Date[]{date1,date2};
}

public static Date[] getTwoDatesWithinThreeHours()
{
    Calendar calendar = Calendar.getInstance();
    Date date1 = calendar.getTime();
    calendar.add(Calendar.HOUR,3);
    Date date2 = calendar.getTime();
    return new Date[]{date1,date2};
}

public static boolean findIfDatesWithinThreeHours(Date date1, Date date2) {
    // here the time of a single hour is defined as 60*60*1000
    System.out.println(date1);
    System.out.println(date2);
    return Math.abs(date1.getTime()-date2.getTime()) <= 3*60*60*1000;

}
}

Your Question is vague. This might get you pointed in the right direction.

If by "timestamp" you meant a count of milliseconds since the epoch of 1970 UTC, construct an Instant . This class represents a moment on the timeline in UTC with a resolution of nanoseconds (finer than milliseconds).

Instant instant = Instant.ofEpochMilli( millis );

Get current moment.

Instant now = Instant.now();

Calculate three hours.

Instant in3Hours = now.plus( 3 , ChronoUnit.HOURS );

See if your moment lies between now and three hours later.

Boolean contained = ( ( ! instant.isBefore( now ) ) && instant.isBefore( in3Hours ) );

If you meant you want to compare a pair of moments to see if the elapsed time is less than 3 hours, use a Duration . This class represents a span of time in terms of seconds and nanoseconds.

Instant earlierInstant = … ;
Instant laterInstant = … ;
Duration duration = Duration.between( earlierInstant , laterInstant );
if ( duration.isNegative() ) { 
    … handle error of unexpected data inputs where the second instant is *before* the first instant.
}
… else …
Boolean elapsedUnderThreeHours = ( duration.compareTo( Duration.ofHours( 3 ) ) == -1 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM