/**
 * 文件名：DateUtil.java
 * 描述：日期/时间工具类
 * 包名：cn.com.kmlt.monstarBase.utils.Date
 * 作者：WENJQ
 * 创建日期：2016年05月26日
 * 
 */
package com.mgq.base.utils.date;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.TimeZone;


public class DateUtil {
	public DateUtil() {

	}

	/**
	 * 
	 * milestotime:时间戳转成标准时间. <br/>
	 * 
	 * @author WENJQ
	 * @param time
	 *            时间戳
	 * @return String time (mm：ss)
	 */
	public String milestotime(int time) {
		Date vketime = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
		return sdf.format(vketime);
	}

	/**
	 * 
	 * formatMsgTime:消息中时间戳转成标准时间. <br/>
	 * 
	 * @author WENJQ
	 * @param time
	 *            时间戳
	 * @return String time (xxxx年xx月xx日 hh:mm)
	 */
	public String formatMsgTime(long time) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(time);
		Formatter ft = new Formatter(Locale.CHINA);
		return ft.format("%1$tY年%1$tm月%1$td日  %1$tT ", cal).toString();
	}

	/**
	 * 
	 * formatMsgTime2:消息中时间戳转成标准时间. <br/>
	 * 
	 * @author WENJQ
	 * @param time
	 *            时间戳
	 * @return String yyyy-MM-dd HH:mm:ss
	 */
	public String formatMsgTime2(long time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date(time)).toString();
	}

	/**
	 * 
	 * formatString2Timestamp:将指定日期转化为时间戳 <br/>
	 * 
	 * @author WENJQ
	 * @param msg
	 *            一个String 的时间
	 * @return long 时间戳
	 */
	public long formatString2Timestamp(String msg) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = df.parse(msg);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return new Date().getTime();
		}
		return date.getTime();
	}



	/**
	 * 
	 * formatMsgTimeStr:yyyy-MM-dd HH:mm:ss->yyyy年MM月dd日 HH:mm:ss. <br/>
	 * 
	 * @author WENJQ
	 * @param time
	 *            A String type of yyyy-MM-dd HH:mm:ss date format
	 * @return time A String type of yyyy年MM月dd日 HH:mm:ss date format
	 * @throws ParseException
	 */
	public String formatMsgTimeStr(String time) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date today = sdf.parse(time);
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss",
				Locale.CHINA);
		return df.format(today).toString();
	}

	/**
	 * 
	 * formatAppUpdateTimeStr: yyyy-MM-dd HH:mm:ss->yyyy年MM月dd日. <br/>
	 * 
	 * @author WENJQ
	 * @param time
	 *            A String type of yyyy-MM-dd HH:mm:ss date format
	 * @return time A String type of yyyy年MM月dd日 date format
	 * @throws ParseException
	 */
	public String formatAppUpdateTimeStr(String time) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date today = sdf.parse(time);
		SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);
		return df.format(today).toString();
	}

	/**
	 * 
	 * formatDateToString:Date to string. <br/>
	 * 
	 * @author WENJQ
	 * @param date
	 * @return str:("yyyy.MM.dd HH:mm");
	 */
	public static String formatDateToString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
		return sdf.format(date).toString();
	}

	/**
	 * 
	 * formatDateToString:Date to string. <br/>
	 * 
	 * @author WENJQ
	 * @param date
	 * @return str:("yyyy.MM.dd HH:mm");
	 */
	public static String dateToYYYYMMDD(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		return sdf.format(date).toString();
	}
	
	   public static String dateToYYYYMM(Date date) {
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
	        return sdf.format(date).toString();
	    }

	public static String dateToYYYYMMDDHHMMSS(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
		return sdf.format(date).toString();
	}



	/**
	 * 
	 * formatMsgTime2:消息中时间戳转成标准时间. <br/>
	 * 
	 * @author WENJQ
	 * @param time
	 *            时间戳
	 * @return String yyyy-MM-dd HH:mm:ss
	 */
	public String formatMsgTime3(long time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return df.format(new Date(time)).toString();
	}

	public String getWeek(String pTime) {


		String Week = "周";


		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		try {

			c.setTime(format.parse(pTime));

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			Week += "天";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 2) {
			Week += "一";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 3) {
			Week += "二";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 4) {
			Week += "三";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 5) {
			Week += "四";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 6) {
			Week += "五";
		}
		if (c.get(Calendar.DAY_OF_WEEK) == 7) {
			Week += "六";
		}



		return Week;
	}

	/**
	 * convertTime:将UTC时间转换为当前时区的时间 <br/>
	 * @param srcTime 时间戳
	 * @param timezone 当前时区
	 * @return String yyyy-MM-dd HH:mm:ss
	 */
	@SuppressLint("SimpleDateFormat")
	public static String convertTimeToTimeZone(String srcTime, TimeZone timezone) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
		SimpleDateFormat dspFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String convertTime;
		Date result_date;
		long result_time = 0;
		// 如果传入参数异常，使用本地时间
		if (null == srcTime) {
			result_time = System.currentTimeMillis();
		} else {
			try { // 将输入时间字串转换为UTC时间
				sdf.setTimeZone(TimeZone.getTimeZone("GMT00:00"));
				result_date = sdf.parse(srcTime);
				result_time = result_date.getTime();
			} catch (Exception e) { // 出现异常时，使用本地时间
				result_time = System.currentTimeMillis();
				dspFmt.setTimeZone(TimeZone.getDefault());
				convertTime = dspFmt.format(result_time);
				return convertTime;
			}
		}
		// 设定时区
		dspFmt.setTimeZone(timezone);
		convertTime = dspFmt.format(result_time);
		return convertTime;
	}

	/**
	 * getCurrentTimeZone:获取当前所在的时区. <br/>
	 * @return String 例如：GMT+08:00
	 */
	public static String getCurrentTimeZone()
	{
		TimeZone tz = TimeZone.getDefault();
		return createGmtOffsetString(true, true, tz.getRawOffset());
	}

	/**
	 * formatMsgTime2:消息中时间戳转成标准时间. <br/>
	 * @param includeGmt	返回时区包含GMT
	 * @param includeMinuteSeparator 返回时区包含分钟分隔符':'
	 * @param offsetMillis	当前时区的偏移时间，单位ms
	 * @return String yyyy-MM-dd HH:mm:ss
	 */
	public static String createGmtOffsetString(boolean includeGmt,
											   boolean includeMinuteSeparator, int offsetMillis) {
		int offsetMinutes = offsetMillis / 60000;
		char sign = '+';
		if (offsetMinutes < 0) {
			sign = '-';
			offsetMinutes = -offsetMinutes;
		}
		StringBuilder builder = new StringBuilder(9);
		if (includeGmt) {
			builder.append("GMT");
		}
		builder.append(sign);
		appendNumber(builder, 2, offsetMinutes / 60);
		if (includeMinuteSeparator) {
			builder.append(':');
		}
		appendNumber(builder, 2, offsetMinutes % 60);
		return builder.toString();
	}

	private static void appendNumber(StringBuilder builder, int count, int value) {
		String string = Integer.toString(value);
		for (int i = 0; i < count - string.length(); i++) {
			builder.append('0');
		}
		builder.append(string);
	}

}
