DateTools

Die Klasse DateTools stellt statische Hilfsmethoden Methoden zur Verfügung, welche immer wieder benötigt werden. So z.B. das Vergleichen von Datum, das Erstellen und Berechnen von Datumstypen oder das Parsen von Datumstpen.

DateTools.java


package de.snowbits.tools;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class DateTools {

	private DateTools() {
	}

	public static final Calendar getDateUTC(Date date) {
		if (date != null) {
			Calendar cal = Calendar.getInstance();
			cal.setTimeZone(TimeZone.getTimeZone("UTC"));
			cal.setTime(date);

			return cal;
		}

		return null;
	}
	
	public static final Date getDateWithoutTime(java.util.Date date) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(date);

		return getDateWithoutTime(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH), cal.get(Calendar.YEAR));
	}
	
	public static final Date getDateWithoutTime(int day, int month, int year) {
		Calendar cal = new GregorianCalendar();
		cal.clear();
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.YEAR, year);
		
		return cal.getTime();
	}	
	
	public static boolean equalsDateWithoutTime(Calendar cal1, Calendar cal2) {
		return (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) && 
				(cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) && 
				(cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH));
	}	
		
	/**
	 * @param date
	 * @param calendar
	 * @return Format: dd.mm.yyyy
	 */
	public static String currentDate(Date date, GregorianCalendar calendar) {
		calendar.setTime(date);
		return parseLength(calendar.get(Calendar.DATE)) + "." + 
			   parseLength((calendar.get(Calendar.MONTH)+1)) + "." + 
			   calendar.get(Calendar.YEAR);
	}	
	
	/**
	 * @param date
	 * @param calendar
	 * @param timeMillis wenn true Format: hh:mm:ss:ms
	 * @return Format: hh:mm:ss 
	 */
	public static  String currentTime(Date date, GregorianCalendar calendar, boolean timeMillis) {
		calendar.setTime(date);
		String stime = parseLength(calendar.get(Calendar.HOUR_OF_DAY)) + ":" + 
					   parseLength(calendar.get(Calendar.MINUTE)) + ":" + 
					   parseLength(calendar.get(Calendar.SECOND));
		if (timeMillis) {
			stime += ":" + calendar.get(Calendar.MILLISECOND);
		}
		return stime;
	}		
	
	public static boolean isLeapYear(int year) {
		GregorianCalendar date = new GregorianCalendar();
		if (date.isLeapYear(year))
			return true;

		return false;
	}
	
	public static final Date getSubtractionDate(Date fromDate, int subtraction) {
		Calendar calFrom = new GregorianCalendar();	
		calFrom.setTime(fromDate);	
		
		if (subtraction != 0) {
			calFrom.add(Calendar.DAY_OF_MONTH, (subtraction > 0 ? subtraction - subtraction * 2 : subtraction));
		}
		
		return calFrom.getTime();
	}
	
	public static final Date getSubtractionDateWithoutTime(Date fromDate, int subtraction) {
		Calendar calFrom = new GregorianCalendar();	
		calFrom.setTime(getDateWithoutTime(fromDate));	
		
		if (subtraction != 0) {
			calFrom.add(Calendar.DAY_OF_MONTH, (subtraction > 0 ? subtraction - subtraction * 2 : subtraction));
		}
		
		return calFrom.getTime();
	}
	
	public static final Date getAddDate(Date fromDate, int addend) {
		Calendar calFrom = new GregorianCalendar();	
		calFrom.setTime(fromDate);	
		
		if (addend != 0) {
			calFrom.add(Calendar.DAY_OF_MONTH, addend);
		}
		
		return calFrom.getTime();
	}	
	
	public static final Date getAddDateWithoutTime(Date fromDate, int addend) {
		Calendar calFrom = new GregorianCalendar();	
		calFrom.setTime(getDateWithoutTime(fromDate));	
		
		if (addend != 0) {
			calFrom.add(Calendar.DAY_OF_MONTH, addend);
		}
		
		return calFrom.getTime();
	}	
	
	public static final boolean equalsWithoutTime(Date dateOne, Date dateTwo) {
		Calendar calOne = new GregorianCalendar();
		calOne.setTime(dateOne);
		Calendar calTwo = new GregorianCalendar();
		calTwo.setTime(dateTwo);
		
		if (calOne.get(Calendar.YEAR) == calTwo.get(Calendar.YEAR) &&
				calOne.get(Calendar.MONTH) == calTwo.get(Calendar.MONTH) &&
				calOne.get(Calendar.DAY_OF_MONTH) == calTwo.get(Calendar.DAY_OF_MONTH)) 
			return true;
		
		return false;
	}
	
	/**
	 * Converts an SQL string into a Date object.
	 * 
	 * @param sqlDateFormat format YYYY-MM-DD HH:MM:SS
	 * @return
	 */
	public static final java.util.Date parseSqlDate(String sqlDateFormat) {
		String error = "Error illegal argument for sqlDateFormat="
				+ sqlDateFormat
				+ " (Explain: allowed format YYYY-MM-DD HH:MM:SS)";
		String parse = sqlDateFormat.trim();

		if (parse.length() != 19)
			throw new IllegalArgumentException(error);

		if (parse.indexOf(" ") == -1 || parse.indexOf("-") == -1
				|| parse.indexOf(":") == -1)
			throw new IllegalArgumentException(error);

		String[] parseStirng = parse.split(" ");
		if (parseStirng.length != 2)
			throw new IllegalArgumentException(error);

		String[] parseDate = parseStirng[0].split("-");
		if (parseDate.length != 3)
			throw new IllegalArgumentException(error);

		String[] parseTime = parseStirng[1].split(":");
		if (parseTime.length != 3)
			throw new IllegalArgumentException(error);

		int[] arrDate = new int[3];
		fillDateOrTime(error, parseDate, arrDate);

		if (arrDate[0] < 1000 || arrDate[0] > 9999 || arrDate[1] < 1
				|| arrDate[1] > 12 || arrDate[2] < 1 || arrDate[2] > 31)
			throw new IllegalArgumentException(error);

		if ((arrDate[1] == 4 || arrDate[1] == 6 || arrDate[1] == 9 || arrDate[1] == 11)
				&& arrDate[2] > 30)
			throw new IllegalArgumentException(error);

		if (isLeapYear(arrDate[0])) {
			if ((arrDate[1] == 2 && arrDate[2] > 29))
				throw new IllegalArgumentException(error);
		} else {
			if ((arrDate[1] == 2 && arrDate[2] > 28))
				throw new IllegalArgumentException(error);
		}

		int[] arrTime = new int[3];
		fillDateOrTime(error, parseTime, arrTime);

		if (arrTime[0] > 23 || arrTime[2] > 59 || arrTime[2] > 23)
			throw new IllegalArgumentException(error);

		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Calendar.YEAR, arrDate[0]);
		cal.set(Calendar.MONTH, arrDate[1] - 1);
		cal.set(Calendar.DAY_OF_MONTH, arrDate[2]);
		cal.set(Calendar.HOUR_OF_DAY, arrTime[0]);
		cal.set(Calendar.MINUTE, arrTime[1]);
		cal.set(Calendar.SECOND, arrTime[2]);

		return cal.getTime();
	}

	public static final String calcRuntime(Date startDate, Date endDate){
		long diff = endDate.getTime() - startDate.getTime();
		long secondsMillis = 1000;
		long minutesMillis = secondsMillis * 60;
		long hoursMillis = minutesMillis * 60;
		long daysMillis = hoursMillis * 24;
 
		diff = diff % daysMillis;
 
		long hours = diff / hoursMillis;
		diff = diff % hoursMillis;
 
		long minutes = diff / minutesMillis;
		diff = diff % minutesMillis;
 
		long seconds = diff / secondsMillis;
		
		return parseLength(hours) + ":" + parseLength(minutes) + ":" + parseLength(seconds);
	}	

	private static void fillDateOrTime(String error, String[] parseDateOrTime,
			int[] arrDateOrTime) {
		for (int i = 0; i < arrDateOrTime.length; i++) {
			arrDateOrTime[i] = StringTools.parseInt(parseDateOrTime[i], -1);
			if (arrDateOrTime[i] < 0)
				throw new IllegalArgumentException(error);
		}
	}
	
	private static String parseLength(long num) {
		return num < 10 ? "0"+ String.valueOf(num) : String.valueOf(num);
	}

	private static String parseLength(int num) {
		return num < 10 ? "0"+ String.valueOf(num) : String.valueOf(num);
	}
	
}
	

Top


Example
Sitemap Kontakt Impressum