package org.openforis.collect.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @author S. Ricci
 *
 */
public class Dates {

	private static final String LOCAL_DATE_TIME_FORMAT 	= "yyyy-MM-dd'T'HH:mm:ss";
	private static final String DATE_TIME_FORMAT 	= "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
	private static final String DATE_FORMAT 		= "yyyy-MM-dd";

	public static Date parseDateTime(String dateTime) {
		if ( StringUtils.isBlank(dateTime) ) {
			return null;
		}
		dateTime = dateTime.trim();
		int length = dateTime.length();
		if ( length == DATE_FORMAT.length() ) {
			return parse(dateTime, DATE_FORMAT);
		} else if (length > LOCAL_DATE_TIME_FORMAT.length()) {
			return parseDateTimeInternal(dateTime);
		} else {
			return parse(dateTime, LOCAL_DATE_TIME_FORMAT);
		}
	}

	private static Date parseDateTimeInternal(String dateTime) {
		int lastIndexOfC = dateTime.lastIndexOf(":");
		dateTime = dateTime.substring(0, lastIndexOfC) + dateTime.substring(lastIndexOfC + 1, dateTime.length());
		return parse(dateTime, DATE_TIME_FORMAT);
	}
	
	public static Date parseDate(String date) {
		if ( StringUtils.isBlank(date) ) {
			return null;
		}
		date = date.trim();
		if ( date.length() > DATE_FORMAT.length() ) {
			date = date.substring(0, DATE_FORMAT.length());
		}
		return parse(date, DATE_FORMAT);
	}

	public static Date parse(String dateString, String format) {
		if ( dateString == null ) {
			return null;
		} else {
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			try {
				return formatter.parse(dateString);
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}
		}
	}
	
	public static String formatDate(Date dateTime) {
		return format(dateTime, DATE_FORMAT);
	}
	
	public static String formatLocalDateTime(Date dateTime) {
		return format(dateTime, LOCAL_DATE_TIME_FORMAT);
	}
	
	public static String formatDateTime(Date dateTime) {
		String value = format(dateTime, DATE_TIME_FORMAT);
		if (value == null) {
			return null;
		}
		value = value.substring(0, value.length() - 2) + ":" + value.substring(value.length() - 2, value.length());
		return value;
	}

	public static String format(Date dateTime, String format) {
		if ( dateTime == null ) {
			return null;
		} else {
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			String result = formatter.format(dateTime);
			return result;
		}
	}

	public static int compareDateOnly(Date date1, Date date2) {
		Date onlyDate1 = toOnlyDate(date1);
		Date onlyDate2 = toOnlyDate(date2);
		return onlyDate1.compareTo(onlyDate2);
	}

	public static Date toOnlyDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
}

@@ -2759,32 +2759,235 @@
 yDate2);%0D%0A%09%7D%0D%0A%0D%0A
+%09public static int compareUpToMinutesOnly(Date date1, Date date2) %7B%0D%0A%09%09Date dt1 = toOnlyDateAndTimeToMinutes(date1);%0D%0A%09%09Date dt2 = toOnlyDateAndTimeToMinutes(date2);%0D%0A%09%09return dt1.compareTo(dt2);%0D%0A%09%7D%0D%0A%0D%0A
 %09public static D
@@ -3249,11 +3249,234 @@
 %0A%09%7D%0D%0A%09%0D%0A
+%09public static Date toOnlyDateAndTimeToMinutes(Date date) %7B%0D%0A%09%09Calendar cal = Calendar.getInstance();%0D%0A%09%09cal.setTime(date);%0D%0A%09%09cal.set(Calendar.SECOND, 0);%0D%0A%09%09cal.set(Calendar.MILLISECOND, 0);%0D%0A%09%09return cal.getTime();%0D%0A%09%7D%0D%0A
 %7D%0D%0A

Add helper function