1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.core.rolling;
12  
13  import java.util.Calendar;
14  import java.util.Date;
15  
16  public class DelayerUtil {
17    
18    // delay until millis in the next second
19    static void delayUntilNextSecond(int millis) {
20      long now = System.currentTimeMillis();
21      Calendar cal = Calendar.getInstance();
22      cal.setTime(new Date(now));
23  
24      cal.set(Calendar.MILLISECOND, millis);
25      cal.add(Calendar.SECOND, 1);
26  
27      long next = cal.getTime().getTime();
28  
29      try {
30        Thread.sleep(next - now);
31      } catch (Exception e) {
32      }
33    }
34  
35    static void delayUntilNextMinute(int seconds) {
36      long now = System.currentTimeMillis();
37      Calendar cal = Calendar.getInstance();
38      cal.setTime(new Date(now));
39  
40      cal.set(Calendar.SECOND, seconds);
41      cal.add(Calendar.MINUTE, 1);
42  
43      long next = cal.getTime().getTime();
44  
45      try {
46        Thread.sleep(next - now);
47      } catch (Exception e) {
48      }
49    }
50  }