What is the most efficient way to check if two long values ​​(in milliseconds) belong to the same second

I am currently using TimeUnit.MILLISECONDS.toSeconds(valueInMillis)to check if two milliseconds happen from the same second. Can you recommend a faster algorithm for this operation?

Thank.

+3
source share
3 answers

This is the code for TimeUnit.MILLISECONDS.toSecond(long d):

public long toSeconds(long d) { return d/(C3/C2); }

where C2, C3are the static constants. You can save one unit ... In this case, I prefer your actual code, it is easier to understand

+3
source

1000. /, . , .

+5

Assuming t1 and t2 are timestamps. For example, from System.currentTimeMillis ()

public static boolean isSameSecond(long t1, long t2){
  return (t1/1000) == (t2/1000)
}
+1
source

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


All Articles