/*
* Copyright 2013 Simon Thiel
*
* This file is part of SitJar.
*
* SitJar is free software: you can redistribute it and/or modify
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SitJar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with SitJar. If not, see <http://www.gnu.org/licenses/lgpl.txt>. *
*/
package sit.tools;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* @author simon
*/
public class DateHelper {
private static final SimpleDateFormat SIMPLE_DATE_FORMAT_SHORT = new SimpleDateFormat("dd.MM.yyyy");
private static final SimpleDateFormat SIMPLE_DATE_FORMAT_LONG = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
/**
* increments date by days
*
* @param date initial date to be changed
* @param days +/- days to be added/removed - days will be removed when days
* is negative
* @return updated date
*/
public static Date addDays(Date date, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, days);
return cal.getTime();
}
public static Date setTimeForDate(Date date, int h, int m, int s) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, h);
cal.set(Calendar.MINUTE, m);
cal.set(Calendar.SECOND, s);
return cal.getTime();
}
public static Date setTimeForDate(Date date, Date time) {
Calendar cal = Calendar.getInstance();
cal.setTime(time);
return setTimeForDate(date, cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
}
public static boolean dateAreOnSameDay(Date d1, Date d2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(d1);
cal2.setTime(d2);
return (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR))
&& (cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}
public static String getShortDate(Date date){
return (SIMPLE_DATE_FORMAT_SHORT).format(date);
}
public static String getLongDate(Date date){
return (SIMPLE_DATE_FORMAT_LONG).format(date);
}
}