package common.utility;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {
	
	public static final int MONDAY = 1;
	public static final int TUESDAY = 2;
	public static final int WEDNESDAY = 3;
	public static final int THURSDAY = 4;
	public static final int FRIDAY = 5;
	public static final int SATURDAY = 6;
	public static final int SUNDAY = 7;
	
	public static Date parse(Object o) {
		if (o instanceof Date)
			return (Date)o;
		if (o instanceof String) {
			try {
				DateFormat df = new SimpleDateFormat("dd/mm/yyyy", Locale.ITALIAN); 
				return df.parse((String) o);
			} catch (Exception ex) {
				return null;
			}
		}
		if (o instanceof Long) {
			return new Date((Long)o);		
		}
		return null;
	}
	
	public static Date addDays(Date date, int days) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, days); 
		return c.getTime();
	}
	
	public static boolean isDate (Object o) {
		if (o instanceof Date)
			return true;
		if (o instanceof String) {
			try {
				DateFormat df = new SimpleDateFormat("dd/mm/yyyy", Locale.ITALIAN); 
				df.parse((String) o);
				return true;
			} catch (Exception ex) {
				return false;
			}
		}
		return false;
	}
}
