/**
 * Copyright 2009-2012 OpenAIRE PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.data.udm.utils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.servlet.http.HttpServletResponse;

/**
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 *
 */
public class DateUtils {

	/** The Constant DATE_FORMAT. */
	private static final String DATE_FORMAT = "yyyy-MM-dd";
	
	/** The Constant TIME_ZONE. */
	private static final String TIME_ZONE = "Europe/Berlin";
	
	/**
	 * Verify date.
	 * 
	 * @param date the date
	 * @param resp the resp
	 * 
	 * @return true, if successful
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static boolean verifyDate(String date, HttpServletResponse resp) 
		throws IOException {
		
		if (date.length() != 10 | !date.contains("-") )
			return false;
		return true;
	}
	
	/**
	 * Compare dates.
	 * 
	 * @param dateFrom the date from
	 * @param dateUntil the date until
	 * 
	 * @return true, if successful
	 * 
	 * @throws ParseException the parse exception
	 */
	public static boolean compareDates(String dateFrom, String dateUntil) 
		throws ParseException {
		
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		Date dFrom = sdf.parse(dateFrom);
    	Date dUntil = sdf.parse(dateUntil);
    	
	    if (dUntil.before(dFrom))  {
	    	return false;
	    }
	    
	    return true;
	}
	
	public static String getCurrentDate () {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		sdf.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
		return sdf.format(date);
	}
}
