View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.util.Calendar;
17  import java.util.Date;
18  
19  public class TimeUtil {
20  
21    
22    public static long computeStartOfNextSecond(long now) {
23      Calendar cal = Calendar.getInstance();
24      cal.setTime(new Date(now));
25      cal.set(Calendar.MILLISECOND, 0);
26      cal.add(Calendar.SECOND, 1);
27      return cal.getTime().getTime();
28    }
29  
30    public static long computeStartOfNextMinute(long now) {
31      Calendar cal = Calendar.getInstance();
32      cal.setTime(new Date(now));
33      cal.set(Calendar.MILLISECOND, 0);
34      cal.set(Calendar.SECOND, 0);
35      cal.add(Calendar.MINUTE, 1);
36      return cal.getTime().getTime();
37    }
38  
39    public static long computeStartOfNextHour(long now) {
40      Calendar cal = Calendar.getInstance();
41      cal.setTime(new Date(now));
42      cal.set(Calendar.MILLISECOND, 0);
43      cal.set(Calendar.SECOND, 0);
44      cal.set(Calendar.MINUTE, 0);
45      cal.add(Calendar.HOUR, 1);
46      return cal.getTime().getTime();
47    }
48    
49    public static long computeStartOfNextDay(long now) {
50      Calendar cal = Calendar.getInstance();
51      cal.setTime(new Date(now));
52  
53      cal.add(Calendar.DAY_OF_MONTH, 1);
54      cal.set(Calendar.MILLISECOND, 0);
55      cal.set(Calendar.SECOND, 0);
56      cal.set(Calendar.MINUTE, 0);
57      cal.set(Calendar.HOUR_OF_DAY, 0);
58      return cal.getTime().getTime();
59    }
60    
61    public static long computeStartOfNextWeek(long now) {
62      Calendar cal = Calendar.getInstance();
63      cal.setTime(new Date(now));
64      
65      cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
66      cal.set(Calendar.HOUR_OF_DAY, 0);
67      cal.set(Calendar.MINUTE, 0);
68      cal.set(Calendar.SECOND, 0);
69      cal.set(Calendar.MILLISECOND, 0);
70      cal.add(Calendar.WEEK_OF_YEAR, 1);
71      return cal.getTime().getTime();
72    }
73  
74    public static long computeStartOfNextMonth(long now) {
75      Calendar cal = Calendar.getInstance();
76      cal.setTime(new Date(now));
77  
78      cal.set(Calendar.DATE, 1);
79      cal.set(Calendar.HOUR_OF_DAY, 0);
80      cal.set(Calendar.MINUTE, 0);
81      cal.set(Calendar.SECOND, 0);
82      cal.set(Calendar.MILLISECOND, 0);
83      cal.add(Calendar.MONTH, 1);
84      return cal.getTime().getTime();
85    }
86    
87    
88  
89  }