package gaf3.core.util;

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

public final class DateUtil {
	
	public static final long MIN_TIME = 201301010000l;
	public static final long MAX_TIME = 209912312400l;
	public static final long MIN_TIME14 = 20130101000000l;
	public static final long MAX_TIME14 = 20991231240000l;
	public static final int MIN_TIME08 = 19491001;
	public static final int MAX_TIME08 = 20990101;

	private static final DateFormat format14 = new SimpleDateFormat("yyyyMMddHHmmss"); 
	private static final DateFormat format12 = new SimpleDateFormat("yyyyMMddHHmm"); 
	private static final DateFormat format8 = new SimpleDateFormat("yyyyMMdd"); 

	public static Long toLongDate(Date date) {
		if(date == null)
			return null;
		String str = format12.format(date);
		return Long.parseLong(str);
	}
	public static Long toLongDate14(Date date) {
		if(date == null)
			return null;
		String str = format14.format(date);
		return Long.parseLong(str);
	}
	
	
	public static Long toLongDate() {
		Calendar cal = Calendar.getInstance();
		return toLongDate(cal.getTime());
	}

	public static Long toLongDate14() {
		Calendar cal = Calendar.getInstance();
		return toLongDate14(cal.getTime());
	}
	public static Integer toShortDate()
	{
		Calendar cal = Calendar.getInstance();
		return toShortDate(cal.getTime());
	}
	public static Integer toShortDate(Date date)
	{
		if(date == null) return null;
		
		int value = Integer.parseInt(format8.format(date));
		return value;
	}

	public static String toStringDate() {
		Calendar cal = Calendar.getInstance();
		String str = format12.format(cal.getTime());
		return str;
	}
	public static Date parseDate(String value) {
		if(StringUtil.isNullOrEmpty(value))
			return null;
		try {
			if(value.length() == 14)
				return format14.parse(value);
			if(value.length() == 12)
				return format12.parse(value);
			if(value.length() == 8)
				return format8.parse(value);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Date parseDate(long value) {
		return parseDate(Long.toString(value));
	}
	public static String toStringDate14() {
		Calendar cal = Calendar.getInstance();
		String str = format14.format(cal.getTime());
		return str;
	}
	
	public static Long startDateNum14(Date date)
	{
		if(date == null) return null;
		
		long value = Long.parseLong(format8.format(date));
		return value*1000000;
	}
	public static Long endDateNum14(Date date)
	{
		if(date == null) return null;
		
		long value = Long.parseLong(format8.format(date));
		return value*1000000+999999;
	}
	
	public static Long startDateNum12(Date date)
	{
		if(date == null) return null;
		
		long value = Long.parseLong(format8.format(date));
		return value*10000;
	}
	public static Long endDateNum12(Date date)
	{
		if(date == null) return null;
		
		long value = Long.parseLong(format8.format(date));
		return value*10000+9999;
	}
	
	public static Long startDateNum14(long date)
	{
		if(date < MIN_TIME08) return null;
		String str = String.format("%s", date);
		str = str.substring(0, 8);
		
		long value = Long.parseLong(str);
		return value*1000000;
	}
	public static Long endDateNum14(long date)
	{
		if(date < MIN_TIME08) return null;
		String str = String.format("%s", date);
		str = str.substring(0, 8);
		
		long value = Long.parseLong(str);
		return value*1000000+999999;
	}
	
	public static int computeAge(long date){
		String str = Long.toString(date);
		if(str.length() < 4) return 0;
		str = str.substring(0,4);
		int year = Integer.parseInt(str);
		return Calendar.getInstance().get(Calendar.YEAR) - year;
	}

	
}
