package org.coody.framework.util;

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

import org.coody.framework.context.base.BaseLogger;

/**
 * 日期类型与字符串类型相互转换
 */
public class DateUtils {

	private static final BaseLogger logger = BaseLogger.getLoggerPro(DateUtils.class);
	
	/**
	 * yyyy-MM-dd hh:mm:ss
	 */
	public static String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static String getWeek(Date date) {
		final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
				"星期六" };
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (dayOfWeek < 0)
			dayOfWeek = 0;
		return dayNames[dayOfWeek];
	}
	/**
	 * 获取当前日期
	 * @return
	 */
	public static String getDateString(){
		return toString(new Date(),"yyyy-MM-dd");
	}
	/**
	 * 时间增减
	 * @param date
	 * @param changeDay
	 * @return
	 */
	public static Date changeDay(Date date, Integer changeDay) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, changeDay);
		return c.getTime();
	}
	/**
	 * 获取某日开始时间
	 * @return
	 */
	public static Date getDayFirstTime(Date date) {
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTime(date);
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}
	/**
	 * 获取某日结束时间
	 * @param date
	 * @return
	 */
	public static Date getDayLastTime(Date date) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTime(date);
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}
	
	/**
	 * 时间转字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String toString(Date date,String format){
		if(date==null){
			return null;
		}
		SimpleDateFormat sfDate = new SimpleDateFormat(format);
		return sfDate.format(date);
	}

	public static String toString(Date date){
		return toString(date, DATETIME_PATTERN);
	}

	/**
	 * 转换为时间
	 */
	public static Date toDate(Object value){
		if(value==null){
			return null;
		}
		try {
			Class<?> clazz=value.getClass();
			if(clazz.isPrimitive()){
				if(value.toString().length()==13){
					return new Date(Long.valueOf(value.toString()));
				}
			}
			if (StringUtil.isMatcher(value.toString(),
					"\\d{13}")) {
				value =new Date(Long.valueOf(value
						.toString()));
				return (Date) value;
			}
			if (StringUtil.isMatcher(value.toString(),
					"\\d{8}")) {
				value = new SimpleDateFormat("yyyyMMdd").parse(value
						.toString());
				return (Date) value;
			}
			if (StringUtil.isMatcher(value.toString(),
					"\\d{14}")) {
				value = new SimpleDateFormat("yyyyMMddHHmmss").parse(value
						.toString());
				return (Date) value;
			}
			if (StringUtil.isMatcher(value.toString(),
					"\\d{17}")) {
				value = new SimpleDateFormat("yyyyMMddHHmmssSSS").parse(value
						.toString());
				return (Date) value;
			}
			if (StringUtil.isMatcher(value.toString(),
					"[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")) {
				value = new SimpleDateFormat("yyyy-MM-dd").parse(value
						.toString());
				return (Date) value;
			}
			if (StringUtil
					.isMatcher(value.toString(),
							"^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D*")) {
				value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
						.parse(value.toString());
				return (Date) value;
			}
			return (Date) value;
		} catch (Exception e) {
			PrintException.printException(logger, e);
			return null;
		}
	}
	
	public static void main(String[] args) {
	}
	
}
