001package fr.ifremer.adagio.core.dao.technical; 002 003/* 004 * #%L 005 * SIH-Adagio :: Shared 006 * $Id:$ 007 * $HeadURL:$ 008 * %% 009 * Copyright (C) 2012 - 2014 Ifremer 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU Affero General Public License as published by 013 * the Free Software Foundation, either version 3 of the License, or 014 * (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU Affero General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * #L% 024 */ 025 026 027import java.util.Calendar; 028import java.util.Date; 029import java.util.GregorianCalendar; 030 031import org.nuiton.util.DateUtil; 032 033import com.google.common.base.Preconditions; 034 035public class DateUtils extends org.apache.commons.lang3.time.DateUtils{ 036 private static Calendar calendar = new GregorianCalendar(); 037 038 /** 039 * Remove a amount of month to a date 040 * @param date 041 * @param amount the amount to remove, in month 042 * @return a new date (= the given date - amount in month) 043 */ 044 public static Date removeMonth(Date date, int amount) { 045 Preconditions.checkNotNull(date); 046 Preconditions.checkArgument(amount > 0); 047 048 // Compute the start date 049 calendar.setTimeInMillis(date.getTime()); 050 calendar.set(Calendar.HOUR_OF_DAY, 0); 051 calendar.set(Calendar.DAY_OF_MONTH, 1); 052 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)-amount); 053 return calendar.getTime(); 054 } 055 056 /** 057 * Get the number of days between two dates 058 * @param startDate 059 * @param endDate 060 * @return a number of hours 061 */ 062 public static double hoursBetween(Date startDate, Date endDate){ 063 double millis = endDate.getTime() - startDate.getTime(); 064 double hours = millis / (1000 * 60 * 60); 065 return hours; 066 } 067 068 /** 069 * Add to date some hours 070 * @param date 071 * @param amount 072 * @return a date (= date + amount) 073 */ 074 public static Date addHours(Date date, Double amount){ 075 long millis = (long) (date.getTime() + amount.doubleValue() * (1000 * 60 * 60)); 076 return new Date(millis); 077 } 078 079 080 /** 081 * Get the last second time of a day: 23:59:59 (0 millisecond) 082 * @param date 083 * @return 084 */ 085 public static Date lastSecondOfTheDay(Date date) { 086 calendar.setTime(date); 087 calendar.set(Calendar.HOUR_OF_DAY, 23); 088 calendar.set(Calendar.MINUTE, 59); 089 calendar.set(Calendar.SECOND, 59); 090 calendar.set(Calendar.MILLISECOND, 0); 091 return calendar.getTime(); 092 } 093 094 public static int getDifferenceInDays(Date startDate, Date endDate) { 095 return DateUtil.getDifferenceInDays(startDate, endDate); 096 } 097}