001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.util; 015 016import java.util.Calendar; 017import java.util.Date; 018 019public class TimeUtil { 020 021 public static long computeStartOfNextSecond(long now) { 022 Calendar cal = Calendar.getInstance(); 023 cal.setTime(new Date(now)); 024 cal.set(Calendar.MILLISECOND, 0); 025 cal.add(Calendar.SECOND, 1); 026 return cal.getTime().getTime(); 027 } 028 029 public static long computeStartOfNextMinute(long now) { 030 Calendar cal = Calendar.getInstance(); 031 cal.setTime(new Date(now)); 032 cal.set(Calendar.MILLISECOND, 0); 033 cal.set(Calendar.SECOND, 0); 034 cal.add(Calendar.MINUTE, 1); 035 return cal.getTime().getTime(); 036 } 037 038 public static long computeStartOfNextHour(long now) { 039 Calendar cal = Calendar.getInstance(); 040 cal.setTime(new Date(now)); 041 cal.set(Calendar.MILLISECOND, 0); 042 cal.set(Calendar.SECOND, 0); 043 cal.set(Calendar.MINUTE, 0); 044 cal.add(Calendar.HOUR, 1); 045 return cal.getTime().getTime(); 046 } 047 048 public static long computeStartOfNextDay(long now) { 049 Calendar cal = Calendar.getInstance(); 050 cal.setTime(new Date(now)); 051 052 cal.add(Calendar.DAY_OF_MONTH, 1); 053 cal.set(Calendar.MILLISECOND, 0); 054 cal.set(Calendar.SECOND, 0); 055 cal.set(Calendar.MINUTE, 0); 056 cal.set(Calendar.HOUR_OF_DAY, 0); 057 return cal.getTime().getTime(); 058 } 059 060 public static long computeStartOfNextWeek(long now) { 061 Calendar cal = Calendar.getInstance(); 062 cal.setTime(new Date(now)); 063 064 cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); 065 cal.set(Calendar.HOUR_OF_DAY, 0); 066 cal.set(Calendar.MINUTE, 0); 067 cal.set(Calendar.SECOND, 0); 068 cal.set(Calendar.MILLISECOND, 0); 069 cal.add(Calendar.WEEK_OF_YEAR, 1); 070 return cal.getTime().getTime(); 071 } 072 073 public static long computeStartOfNextMonth(long now) { 074 Calendar cal = Calendar.getInstance(); 075 cal.setTime(new Date(now)); 076 077 cal.set(Calendar.DATE, 1); 078 cal.set(Calendar.HOUR_OF_DAY, 0); 079 cal.set(Calendar.MINUTE, 0); 080 cal.set(Calendar.SECOND, 0); 081 cal.set(Calendar.MILLISECOND, 0); 082 cal.add(Calendar.MONTH, 1); 083 return cal.getTime().getTime(); 084 } 085 086}