View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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      public static long computeStartOfNextSecond(long now) {
22          Calendar cal = Calendar.getInstance();
23          cal.setTime(new Date(now));
24          cal.set(Calendar.MILLISECOND, 0);
25          cal.add(Calendar.SECOND, 1);
26          return cal.getTime().getTime();
27      }
28  
29      public static long computeStartOfNextMinute(long now) {
30          Calendar cal = Calendar.getInstance();
31          cal.setTime(new Date(now));
32          cal.set(Calendar.MILLISECOND, 0);
33          cal.set(Calendar.SECOND, 0);
34          cal.add(Calendar.MINUTE, 1);
35          return cal.getTime().getTime();
36      }
37  
38      public static long computeStartOfNextHour(long now) {
39          Calendar cal = Calendar.getInstance();
40          cal.setTime(new Date(now));
41          cal.set(Calendar.MILLISECOND, 0);
42          cal.set(Calendar.SECOND, 0);
43          cal.set(Calendar.MINUTE, 0);
44          cal.add(Calendar.HOUR, 1);
45          return cal.getTime().getTime();
46      }
47  
48      public static long computeStartOfNextDay(long now) {
49          Calendar cal = Calendar.getInstance();
50          cal.setTime(new Date(now));
51  
52          cal.add(Calendar.DAY_OF_MONTH, 1);
53          cal.set(Calendar.MILLISECOND, 0);
54          cal.set(Calendar.SECOND, 0);
55          cal.set(Calendar.MINUTE, 0);
56          cal.set(Calendar.HOUR_OF_DAY, 0);
57          return cal.getTime().getTime();
58      }
59  
60      public static long computeStartOfNextWeek(long now) {
61          Calendar cal = Calendar.getInstance();
62          cal.setTime(new Date(now));
63  
64          cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
65          cal.set(Calendar.HOUR_OF_DAY, 0);
66          cal.set(Calendar.MINUTE, 0);
67          cal.set(Calendar.SECOND, 0);
68          cal.set(Calendar.MILLISECOND, 0);
69          cal.add(Calendar.WEEK_OF_YEAR, 1);
70          return cal.getTime().getTime();
71      }
72  
73      public static long computeStartOfNextMonth(long now) {
74          Calendar cal = Calendar.getInstance();
75          cal.setTime(new Date(now));
76  
77          cal.set(Calendar.DATE, 1);
78          cal.set(Calendar.HOUR_OF_DAY, 0);
79          cal.set(Calendar.MINUTE, 0);
80          cal.set(Calendar.SECOND, 0);
81          cal.set(Calendar.MILLISECOND, 0);
82          cal.add(Calendar.MONTH, 1);
83          return cal.getTime().getTime();
84      }
85  
86  }