/*
 * @(#)TimeFormatTool.java 1.0 2014-11-25
 *
 * Copyright (c) 2013-2014 JiangXi Hangtian PoHuYun(JXHTPHY), Inc. 
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of JiangXi 
 * Hangtian PoHuYun, Inc. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with JXHTPHY.
 *
 * JXHTPHY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. JXHTPHY SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */
package com.base.core.utiles;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * @类名: TimeFormatTool
 * @描述: 提供各种时间格式化操作的公用类
 * @版本:
 * @创建日期: 2014-11-25下午02:51:55
 * @作者: 曾贵荣
 * @JDK: 1.6
 */

public class TimeFormatTool {
	/**
	 * 获取当前时间为yyyy-MM-ddTHH:mm:ss格式的字符串
	 * 
	 * @return TimeNow
	 */
	public static String calendarToString() {
		return calendarToString(Calendar.getInstance());
	}

	/**
	 * 将Calendar类型转换为yyyy-MM-dd HH:mm:ss格式的字符串
	 * 
	 * @param calendar
	 * @return
	 */
	public static String calendarToString(Calendar calendar) {
		if (calendar == null) {
			return "";
		}
		/*SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss.SSS");*/
		SimpleDateFormat formatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss.SSS");
		String result = formatter.format(calendar.getTime());
		// 将日期格式变成"yyyy-MM-ddTHH:mm:ss"
		// result = result.replace(" ", "T");
		return result;
	}

	/**
	 * 将Calendar类型转换为yyyyMMdd格式的字符串
	 * 
	 * @param calendar
	 * @return
	 */
	public static String dayToString() {
		return dayToString(Calendar.getInstance());
	}

	/**
	 * 将Calendar类型转换为yyyyMMdd格式的字符串
	 * 
	 * @param calendar
	 * @return
	 */
	public static String dayToString(Calendar calendar) {
		if (calendar == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String result = formatter.format(calendar.getTime());
		return result;
	}

	public static String dayToStringrw(Calendar calendar) {
		if (calendar == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
		String result = formatter.format(calendar.getTime());
		return result;
	}

	/**
	 * 将Calendar类型转换为yyyy-MM-dd格式的字符串
	 * 
	 * @param calendar
	 * @return
	 */
	public static String dayToString1(Calendar calendar) {
		if (calendar == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String result = formatter.format(calendar.getTime());
		return result;
	}

	/**
	 * 将Calendar类型转换为HHmmss格式的字符串
	 * 
	 * @param calendar
	 * @return
	 */
	public static String timeToString(Calendar calendar) {
		if (calendar == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
		String result = formatter.format(calendar.getTime());
		return result;
	}

	/**
	 * 将Calendar类型转换为HH:mm:ss格式的字符串
	 * 
	 * @param calendar
	 * @return
	 */
	public static String timeToString1(Calendar calendar) {
		if (calendar == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String result = formatter.format(calendar.getTime());
		return result;
	}

	/**
	 * 将Calendar类型转换为format格式的字符串，若格式错误，转换为yyyy-MM-ddTHH:mm:ss格式
	 * 
	 * @param calendar
	 * @return
	 */
	public static String calendarToString(Calendar calendar, String format) {
		if (calendar == null) {
			return "";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try {
			return formatter.format(calendar.getTime());
		} catch (Exception e) {
			return calendarToString(calendar);
		}
	}

	/**
	 * 将yyyy-MM-ddTHH:mm:ss.UUU格式的字符串转换为Calendar类型
	 * 
	 * @param calendarString
	 * @return
	 */
	public static Calendar stringToCalendar(String calendarString) {
		if (calendarString == null || calendarString.trim().length() == 0) {
			return null;
		}
		String dateStr = calendarString;
		if (calendarString.trim().indexOf("T") != -1) {
			dateStr = calendarString.trim().replace("T", " ");
		}
		String dateFormat = "yyyy-MM-dd HH:mm:ss";
		if (dateStr.indexOf(".") != -1) {
			dateFormat += ".SSS";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(formatter.parse(dateStr));
		} catch (ParseException e) {
			return null;
		}
		return calendar;
	}

	public static Calendar stringToCalendar(String calendarString,
			String dateFormat) {
		if (calendarString == null || calendarString.trim().length() == 0) {
			return null;
		}
		String dateStr = calendarString;
		if (calendarString.trim().indexOf("T") != -1) {
			dateStr = calendarString.trim().replace("T", " ");
		}

		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(formatter.parse(dateStr));
		} catch (ParseException e) {
			return null;
		}
		return calendar;
	}

	/**
	 * 将calendar中的T替换为空格
	 * 
	 * @param calendar
	 * @return
	 */
	public static String formatCalendar(String calendar) {
		if (calendar == null) {
			return "";
		}
		return calendar.replace("T", " ");
	}

	/**
	 * 检查日期格式是否正确
	 * 
	 * @param day
	 * @return
	 */
	public static boolean checkDayFormat(String day) {
		if (day == null) {
			return false;
		} else {
			String dateFormat = "yyyy-MM-dd";
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
			try {
				formatter.parse(day);
				return true;
			} catch (ParseException e) {
				return false;
			}
		}
	}

	/**
	 * 检查时间格式是否正确
	 * 
	 * @param time
	 * @return
	 */
	public static boolean checkTimeFormat(String time) {
		if (time == null) {
			return false;
		} else {
			String dateFormat = "yyyy-MM-dd HH:mm:ss";
			if (time.indexOf(".") != -1) {
				dateFormat += ".SSS";
			}
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
			try {
				formatter.parse(time);
				return true;
			} catch (ParseException e) {
				return false;
			}
		}
	}
	/**
	 * 检查时间格式是否正确
	 * 
	 * @param time
	 * @return
	 */
	public static boolean checkTimeFormat2(String time) {
		if (time == null) {
			return false;
		} else {
			String dateFormat = "yyyy-MM-dd HH:mm:ss";
			SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
			try {
				formatter.parse(time);
				return true;
			} catch (ParseException e) {
				return false;
			}
		}
	}

	/**
	 * 将YYYY-MM-DD HH:MM:SS.SSS的字符串变为类型为Calendar的日期
	 * 
	 * @param str_Time
	 *            String 字符串表示的时间
	 * @return Calendar Calendar类型的时间
	 */
	public static Calendar stringtoCalendar(String str_Time) {
		try {
			int year = Integer.parseInt(str_Time.substring(0, 4));
			int month = Integer.parseInt(str_Time.substring(5, 7));
			int day = Integer.parseInt(str_Time.substring(8, 10));
			int hour = Integer.parseInt(str_Time.substring(11, 13));
			int minute = Integer.parseInt(str_Time.substring(14, 16));
			int second = Integer.parseInt(str_Time.substring(17, 19));
			Calendar time = new GregorianCalendar(year, month - 1, day, hour,
					minute, second);
			return time;
		} catch (NumberFormatException e) {
			return null;
		}
	}

	public static String string2Timezone(String srcFormater,
			String srcDateTime, String srcTimeZoneId, String dstFormater,
			String dstTimeZoneId) {
		if (srcFormater == null || "".equals(srcFormater))
			return null;
		if (srcDateTime == null || "".equals(srcDateTime))
			return null;
		if (dstFormater == null || "".equals(dstFormater))
			return null;
		if (dstTimeZoneId == null || "".equals(dstTimeZoneId))
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(srcFormater);
		try {
			int diffTime = getDiffTimeZoneRawOffset(srcTimeZoneId,
					dstTimeZoneId);
			Date d = sdf.parse(srcDateTime);
			long nowTime = d.getTime();
			long newNowTime = nowTime - diffTime;
			d = new Date(newNowTime);
			return date2String(dstFormater, d);
		} catch (ParseException e) {
			return null;
		} finally {
			sdf = null;
		}
	}

	private static int getDiffTimeZoneRawOffset(String srcTimeZoneID,
			String timeZoneId) {
		return TimeZone.getTimeZone(srcTimeZoneID).getRawOffset()
				- TimeZone.getTimeZone(timeZoneId).getRawOffset();
	}

	public static String date2String(String formater,Date date) {
		if (formater == null || "".equals(formater))
			return null;
		if (date == null)
			return null;
		return (new SimpleDateFormat(formater)).format(date);
	}

	public static String string2TimezoneDefault(String srcDateTime,
			String srcTimeZoneId, String dstTimeZoneId) {
		return string2Timezone("yyyy-MM-dd HH:mm:ss.SSS", srcDateTime,
				srcTimeZoneId, "yyyy-MM-dd HH:mm:ss.SSS", dstTimeZoneId);
	}
	
	public static Date string2DateDefault(String srcTime){
		String dateFormat = "yyyy-MM-dd HH:mm:ss";
		if (srcTime.indexOf(".") != -1) {
			dateFormat += ".SSS";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		try {
			Date date = formatter.parse(srcTime);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public static Date string2Date(String srcTime,String format){
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		try {
			Date date = formatter.parse(srcTime);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String dateToStringDefault(Date date){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = formatter.format(date);
		return dateStr;
	}
	public static Date getDayAfter(Date dis_day){
		Calendar c = Calendar.getInstance();
		c.setTime(dis_day);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day+1);
		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		Date dayafter = null;
		try {
			dayafter = new SimpleDateFormat("yyyy-MM-dd").parse(dayAfter);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dayafter;
	}
	
}
