package com.platform.utils;

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

/** 日期转换
 * @author chen
 *
 */
public class DateForm {
	
	/**
	 * 精确到秒 样式1
	 */
	private static final String date_format_second = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 精确到秒 样式2
	 */
	private static final String date_format_second_non = "yyyy-MM-dd_HH-mm-ss";
	
	/**
	 * 精确到分 样式1
	 */
	private static final String data_format_min = "yyyy-MM-dd HH:mm";
	
	/**
	 * 精确到天 样式1
	 */
	private static final String data_format_day = "yyyy-MM-dd";
	
    private static ThreadLocal<DateFormat> threadLocal_second = new ThreadLocal<DateFormat>();
    
    private static ThreadLocal<DateFormat> threadLocal_second_non = new ThreadLocal<DateFormat>();
    
    private static ThreadLocal<DateFormat> threadLocal_min = new ThreadLocal<DateFormat>();
    
    private static ThreadLocal<DateFormat> threadLocal_day = new ThreadLocal<DateFormat>();
 
	public static String date2StringBysecond(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_second.get();
		if(format == null){  
			format = new SimpleDateFormat(date_format_second);  
			threadLocal_second.set(format);  
        }
		return format.format(date);
	}
	
	public static String date2StringBysecondNon(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_second_non.get();
		if(format == null){  
			format = new SimpleDateFormat(date_format_second_non);  
			threadLocal_second_non.set(format);  
        }
		return format.format(date);
	}
	
	public static String date2StringByMin(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_min.get();
		if(format == null){  
			format = new SimpleDateFormat(data_format_min);  
			threadLocal_min.set(format);  
        }
		return format.format(date);
	}
	
	public static String date2StringByDay(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat format = threadLocal_day.get();
		if(format == null){  
			format = new SimpleDateFormat(data_format_day);  
			threadLocal_day.set(format);  
        }
		return format.format(date);
	}
	
	public static Date string2DateBysecond(String date) {
		if (date == null) {
			return null;
		}
		date = date.trim();
		if (date.isEmpty()) {
			return null;
		}
		DateFormat format = threadLocal_second.get();
		try {
			if(format == null){  
				format = new SimpleDateFormat(date_format_second);
				threadLocal_second.set(format);  
			}
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date string2DateByMin(String date) {
		if (date == null) {
			return null;
		}
		date = date.trim();
		if (date.isEmpty()) {
			return null;
		}
		DateFormat format = threadLocal_min.get();
		try {
			if(format == null){  
				format = new SimpleDateFormat(data_format_min);
				threadLocal_min.set(format);  
			}
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date string2DateByDay(String date) {
		if (date == null) {
			return null;
		}
		date = date.trim();
		if (date.isEmpty()) {
			return null;
		}
		DateFormat format = threadLocal_day.get();
		try {
			if(format == null){  
				format = new SimpleDateFormat(data_format_day);
				threadLocal_day.set(format);  
			}
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
