package org.ts.ddcs.common.util;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


public class DateUtil {
	
	//static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 将时间格式转换成符合Timestamp要求的格式.
	//static SimpleDateFormat sdfHour = new SimpleDateFormat("yyyyMMddHH0000");// 将时间格式转换成符合Timestamp要求的格式.
	//static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyyMMdd000000");// 将时间格式转换成符合Timestamp要求的格式.
	//static SimpleDateFormat sdfHour1 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");// 将时间格式转换成符合Timestamp要求的格式.
	//static SimpleDateFormat strToDateSdf = new SimpleDateFormat("yyyyMMddHHmmss");// 将时间格式转换成符合Timestamp要求的格式.

	
    private DateUtil() {

    }

    public static String getSystemDate(String format) {

        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        String str = getDateString(date, format);
        return str;
    }

    public static String getCurrYear() {

        return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    }

    public static String getCurrMonth() {

        int currMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
        return currMonth < 10 ? "0" + currMonth : String.valueOf(currMonth);
    }

    public static String getCurrDay() {

        int currDay = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        return currDay < 10 ? "0" + currDay : String.valueOf(currDay);
    }

    public static String formatDate(String dateString, String oldFormat,
            String newFormat) {

        if ("".equals(dateString.trim())) {
            return "";
        }
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
        Date date = null;
        try {
            date = sdf.parse(dateString);
            sdf = new SimpleDateFormat(newFormat);
            result = sdf.format(date);
        }
        catch (Exception e) {
            result = dateString;
        }
        return result;
    }

    public static Date getDate(String dateString, String format)
            throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateString);
    }

    public static String getDateString(Date date, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String roll(String origin, String pattern, int field,
            int amount) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern(pattern);
        Date date = sdf.parse(origin);

        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.add(field, amount);

        return sdf.format(gc.getTime());
    }

    public static String dateBefAft(int days, String format) {

        if (format == null || "".equals(format)) {
            format = "yyyy/MM/dd hh:mm:ss";
        }
        Calendar now = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        now.add(Calendar.DAY_OF_YEAR, days);
        return formatter.format(now.getTime());
    }

    public static String secondTimeBefAft(int second, String format) {

        if (format == null || "".equals(format)) {
            format = "yyyy/MM/dd hh:mm:ss";
        }
        Calendar now = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        now.add(Calendar.SECOND, second);
        return formatter.format(now.getTime());
    }

    public static String miniteTimeBefAft(int minute, String format) {

        if (format == null || "".equals(format)) {
            format = "yyyy/MM/dd hh:mm:ss";
        }
        Calendar now = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        now.add(Calendar.MINUTE, minute);
        return formatter.format(now.getTime());
    }

    public static boolean compareWithNowTime(String dateTime)
            throws ParseException {

        Calendar now = Calendar.getInstance();
        Calendar c1 = Calendar.getInstance();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        c1.setTime(formatter.parse(dateTime));
        return now.compareTo(c1) > 0;
    }
    
    public static String getLastDayOfMonth(String year, String month) throws Exception {
        
        String day = "";
        switch (Integer.parseInt(month)) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = "31";
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                day = "30";
                break;
            case 2:
                int nYear = Integer.parseInt(year);
                if ((nYear % 4 == 0 && nYear % 100 != 0) || nYear % 400 == 0) {
                    day = "29";
                } else {
                    day = "28";
                }
                break;
            default:
                break;
        }
        return day;
    }
    
    //获取时间戳
    public static Timestamp getTs_time(){
    	Date date = new Date();// 获得系统时间.
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String nowTime = sdf.format(date);// 将时间格式转换成符合Timestamp要求的格式.
    	Timestamp ts_time = Timestamp.valueOf(nowTime);// 把时间转换
    	return ts_time;
    }
    
   
    /**
     *获取当前整点的时间
     * @return
     */
    public static String getCurrentHour(){

        SimpleDateFormat sdfHour = new SimpleDateFormat("yyyyMMddHH0000");
        return  sdfHour.format(new Date());
    }
    
    /**
     *获取当前整点的时间
     * @return
     */
    public static String getCurrentHour1(){
        SimpleDateFormat sdfHour1 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		return  sdfHour1.format(new Date());
    }
    
    /**
     *获取当前时间 preHour个小时前整点的时间
     * @return
     */
    public static String getPreHour(int preHour){
    	Calendar calendar = Calendar.getInstance();  /* HOUR_OF_DAY 指示一天中的小时 */
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - preHour);
        SimpleDateFormat sdfHour = new SimpleDateFormat("yyyyMMddHH0000");
		return  sdfHour.format(calendar.getTime());
    }
    
    /**
     *获取指定日期的的钱preHour个小时前的时间
     * @return
     */
    public static String getPreHourTime(String dateStr , int preHour){
		
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
	    	Date d = df.parse(dateStr);
	    	Calendar calendar = Calendar.getInstance();// HOUR_OF_DAY 指示一天中的小时 
			calendar.setTime(d);
			calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - preHour);		
			return  df.format(calendar.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return  dateStr;
    }
    
    /**
     *获取当前时间 preHour个小时前整点的时间
     * @return
     */
    public static String getPreHour1(int preHour){
    	Calendar calendar = Calendar.getInstance();  /* HOUR_OF_DAY 指示一天中的小时 */
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - preHour);
        SimpleDateFormat sdfHour1 = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		return  sdfHour1.format(calendar.getTime());
    }
    
    /**
     * 获取当前时间 30天前的整点的时间
     * @return
     */
    public static String getPreMouth(int preHour){
    	Calendar calendar = Calendar.getInstance();  /* HOUR_OF_DAY 指示一天中的小时 */
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 30);
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - preHour);
        SimpleDateFormat sdfHour = new SimpleDateFormat("yyyyMMddHH0000");
		return  sdfHour.format(calendar.getTime());
    }
 
    /**
     * 获取当前时间 preNum天前的整天的时间
     * @return
     */
    public static String getPreDays(int preNum){
    	Calendar calendar = Calendar.getInstance();  /* HOUR_OF_DAY 指示一天中的小时 */
		calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - preNum);
        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyyMMdd000000");
		return  sdfDay.format(calendar.getTime());
    }
    
    /**
     * 计算俩个时间之间相差多少秒
     * @param preTime
     * @param afterTime
     * @return
     */
    public static long getTimeMinusSecond(String preTime,String afterTime) throws ParseException {
    	try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date preDate = sdf.parse(preTime);
			Date afterDate = sdf.parse(afterTime);
			long diff =  afterDate.getTime() - preDate.getTime();
			long second = diff/1000;
			return second;
		} catch (ParseException e) {
			throw e;
		}
    }
    
    
    /**
     *将20170808121212转化为2017-08-08 12:12:12
     * @return
     */
    public static String getFormatDateStr(String dateStr){
    	StringBuffer formatDateStr = new StringBuffer();
    	formatDateStr.append(dateStr.substring(0, 4));
    	formatDateStr.append("-");
    	formatDateStr.append(dateStr.substring(4, 6));
    	formatDateStr.append("-");
    	formatDateStr.append(dateStr.substring(6, 8));
    	formatDateStr.append(" ");
    	formatDateStr.append(dateStr.substring(8, 10));
    	formatDateStr.append(":");
    	formatDateStr.append(dateStr.substring(10,12));
    	formatDateStr.append(":");
    	formatDateStr.append(dateStr.substring(12, 14));
    	
		return  formatDateStr.toString();
    }
    
    /**
     * 获取当前时间类型为Timestamp的时间
     * @return
     */
    public static String getCurrTimestamp() {

    	Date date = new Date();// 获得系统时间.
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String nowTime = sdf.format(date);// 将时间格式转换成符合Timestamp要求的格式.
		//Timestamp currTime = Timestamp.valueOf(nowTime);// 把时间转换
        return nowTime;
    }
    
    /**
     * 获取当前时间类型为Timestamp的时间
     * @return
     */
    public static String getCurrTimestampStr() {

    	Date date = new Date();// 获得系统时间.
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String nowTime = sdf.format(date);// 将时间格式转换成符合Timestamp要求的格式.
		//Timestamp currTime = Timestamp.valueOf(nowTime);// 把时间转换
        return nowTime;
    }
    public static void main(String[] args) {
    	//System.out.println(getTimeMinusSecond("2016-01-11 160000","20160111160006"));
    	System.out.println(getFormatDateStr("20160111160006"));
    	
    	/*try {
			Date date = strToDateSdf.parse("20080710192034");
			String str = sdf.format(date);
			System.out.println(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
}
