[concurrency-interest] Re: SynchronizedLong vs. other locks

Tim Peierls tim at peierls.net
Fri Jul 8 10:43:51 EDT 2005


Tim Peierls wrote:
> Dawid Kurzyniec wrote:
> 
>> private static final long DAY = 24*60*60*1000;
>>
>> private static long getTomorrowTime() {
>>    return (((System.currentTimeMillis() + DAY)/DAY)*DAY;
>> }
> 
> 
> Does this deal with leap seconds properly?

Answering my own question: No, it doesn't. The program below produces this 
output on my machine (today):

t1 = 1120881600000
t2 = 1120867200000

a difference of 4 minutes. (Hmm, why four minutes? Only 23 leap seconds 
have been inserted since the epoch.)

--tim


import java.util.*;

public class TomorrowTime {
     private static final long DAY = 24*60*60*1000;

     public static void main(String[] args) {
         System.out.printf("t1 = %d%nt2 = %d%n",
             getTomorrowTime1(),
             getTomorrowTime2());
     }

     private static long getTomorrowTime1() {
         GregorianCalendar cal = new GregorianCalendar();
         cal.add(Calendar.DATE, 1);
         cal.set(Calendar.HOUR_OF_DAY, 0);
         cal.set(Calendar.MINUTE, 0);
         cal.set(Calendar.SECOND, 0);
         cal.set(Calendar.MILLISECOND, 0);
         return cal.getTimeInMillis();
     }

     private static long getTomorrowTime2() {
        return ((System.currentTimeMillis() + DAY)/DAY)*DAY;
     }
}



More information about the Concurrency-interest mailing list