package cn.net.caoyao.cpzshandset.common.util;

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

public class DateUtil {

	public static String getDataForLog() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		return f.format(c.getTime());
	}

	/**
	 * 
	 * @param milliseconds
	 * @return e.g.2016年08月03日 16:02
	 */
	public static String getDateTime(long milliseconds) {
		Date date = new Date(milliseconds);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		return sdf.format(date);
	}
	
	/**
	 * 
	 * @param milliseconds
	 * @return e.g.2016年08月03日 16:02
	 */
	public static String getDateTimeFormat(long milliseconds) {
		Date date = new Date(milliseconds);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return sdf.format(date);
	}
	
	/*public static String testDate(){
		String currentTime=android.text.format.DateFormat.format("yyyy-MM-dd hh-mm-ss", new Date()).toString();  
		currentTime="通过DateFormat获取的时间:\n"+currentTime;       
		//通过SimpleDateFormat获取24小时制时间  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.getDefault());        
		currentTime+="\n"+"通过SimpleDateFormat获取24小时制时间：\n"+sdf.format(new Date());  
		//通过SimpleDateFormat获取12小时制时间  
		sdf=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss", Locale.getDefault());     
		currentTime+="\n"+"通过SimpleDateFormat获取12小时制时间：\n"+sdf.format(new Date()); 
		
		return currentTime;
	}*/
	/**
	 * 日期转换为时间戳
	 * @param time 2016-08-03 16:02:00
	 * @return
	 */
	public static int StringToTimestamp(String time) {
		int times = 0;
		times = (int) ((Timestamp.valueOf(time).getTime()) / 1000);
		if (times == 0) {
			System.out.println("String转10位时间戳失败");
		}
		return times;
	}
	/**
	 * 固定时间转换为时间戳
	 * @param dateStr
	 * @return
	 */
	public static long getTimestamp(String dateStr) {
//		1970年01月18日 08:23
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date.getTime();
	}
	/**
	 * 给定的时间和系统时间是否为当天
	 *
	 * @param scanTime  20161011 111213
	 * @return 同一天:返回时间; 不是同一天:返回日期
	 */
	public static String formatDate(String scanTime) {
		if (StringUtils.isEmpty(scanTime)) return "";
		try {
			SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMdd HHmmss");
			SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = null;
			date = oldFormat.parse(scanTime);
			System.out.println(" oldFormat:" + oldFormat.format(date));
			return newFormat.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return  "";
	}
	
	/**
	 * 给定的时间和系统时间是否为当天
	 *
	 * @param scanTime  20161011111213
	 * @return 同一天:返回时间; 不是同一天:返回日期
	 */
	public static String formatDateNoBlank(String scanTime) {
		if (StringUtils.isEmpty(scanTime)) return "";
		try {
			SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = null;
			date = oldFormat.parse(scanTime);
			System.out.println(" oldFormat:" + oldFormat.format(date));
			return newFormat.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return  "";
	}
	/**
	 * @return yyyy-mm-dd HH:mm格式的日期
	 */
	public static String getDate() {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		return sdf.format(new Date());

	}

	/**
	 * 比较两个日期的大小
	 * @param date1
	 * @param date2
	 * @return 1：date1 > date2    -1:date1 < date2    0:date1 == date2
	 */
	public static int compare_date(String date1, String date2) {


		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (dt1.getTime() > dt2.getTime()) {
//				LogUtil.d(TConfig.ECHO_Time, "dt1在dt2前");
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
//				LogUtil.d(TConfig.ECHO_Time, "dt1在dt2后");
				return -1;
			} else {
//				LogUtil.d(TConfig.ECHO_Time, "dt1和dt2相等");
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
			LogUtil.e("DateUtil","compare Date error " + exception.getLocalizedMessage());
		}
		return 0;
	}

	/**
	 * 
	 * @return yyyy-MM-dd格式的日期
	 */
	public static String getDate1() {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		return sdf.format(new Date());

	}

	/**
	 * 
	 * @return HH:mm:ss
	 */
	public static String getTime() {

		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		return sdf.format(new Date());
	}

	public static String getWeek() {
		String currentWeek = null;
		Calendar c = Calendar.getInstance();
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if (mWay.equals("1")) {
			currentWeek = "星期天";
		} else if (mWay.equals("2")) {
			currentWeek = "星期一";
		} else if (mWay.equals("3")) {
			currentWeek = "星期二";
		} else if (mWay.equals("4")) {
			currentWeek = "星期三";
		} else if (mWay.equals("5")) {
			currentWeek = "星期四";
		} else if (mWay.equals("6")) {
			currentWeek = "星期五";
		} else if (mWay.equals("7")) {
			currentWeek = "星期六";
		}
		return currentWeek;
	}

	/**
	 * 
	 * @param milliseconds
	 *            毫秒数
	 * @return 简单的默认格式日期时间
	 */
	public static String getSimpleDatetime(long milliseconds) {
		Date date = new Date(milliseconds);
		DateFormat sdf = SimpleDateFormat.getDateTimeInstance();
		return sdf.format(date);
	}

	/**
	 * "yyyy-MM-dd HH:mm:ss Z"
	 * 
	 * @param milliseconds
	 *            毫秒数
	 * @return 默认上述格式的时间
	 */
	public static String getDefaultDatetime(long milliseconds) {
		Date date = new Date(milliseconds);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
		return sdf.format(date);
	}

	/**
	 * "yyyy-MM-dd HH:mm"
	 * 
	 * @param milliseconds
	 *            毫秒数
	 * @return 默认上述格式的时间
	 */
	public static String[] getIsThisYearDatetime(long milliseconds) {
		Date date = new Date(milliseconds);
		String pattern;
		if (new Date().getYear() == date.getYear())
			pattern = "MM-dd";
		else
			pattern = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
		return new String[] { sdf.format(date), sdf2.format(date) };
	}

	/**
	 * "yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param milliseconds
	 *            毫秒数
	 * @return 默认上述格式的时间
	 */
	public static String getTimestampDatetime(long milliseconds) {
		Date date = new Date(milliseconds);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}

	/**
	 * "20100315072741000-0700" => "yyyy-MM-dd HH:mm:ss Z" ex.
	 * 可用于Ocean返回的时间格式的转换
	 * 
	 * @param strTime
	 * @return
	 */
	public static String getTimeString(String strTime) {
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSSZ");
			Date d = formatter.parse(strTime);
			DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

			return df1.format(d);
		} catch (ParseException e) {
			return strTime;
		} catch (NullPointerException npe) {
			return "";
		}
	}

	/**
	 * eg:19900101
	 * 
	 * @param datetime 19900101
	 * @return1990-01-01
	 */
	public static String getDateFormat(String datetime) {
		try {
			int year = Integer.parseInt(datetime.substring(0, 4));
			int month = Integer.parseInt(datetime.substring(4, 6));
			int day = Integer.parseInt(datetime.substring(6, 8));

			// System.out.println(datetime + "------------------" + year + ","
			// + month + "," + day);

			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month - 1, day);

			Date date = calendar.getTime();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.format(date);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * eg:19900101
	 * 
	 * @param datetime19900101
	 * @return1990-01-01
	 */
	/*
	 * public static Timestamp getDateFormatToStamp(String datetime) { try {
	 * String date0 = getDateFormat("19900101") + " 00:00:00"; Timestamp ts1 =
	 * Timestamp.valueOf(date0); // DateFormat format = new SimpleDateFormat(
	 * "yyyy/MM/dd HH:mm:ss");
	 * 
	 * // Timestamp ts = new Timestamp(date.getTime()); return ts1;
	 * 
	 * } catch (Exception e) { // TODO: handle exception } return null; }
	 */
	/**
	 * 将时间戳格式的时间转换为当前时间
	 * 
	 * @param datetime
	 *            315504000
	 * @return 1980-01-01
	 */
	public static String getTimeStampFormat(String datetime) {

		try {
			Date date = new Date(Long.parseLong(datetime) * 1000);
			// System.out.println("------------" + datetime + "-----"
			// + date.toLocaleString());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			return sdf.format(date);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 将时间戳格式的时间转换为当前时间
	 * 
	 * @param datetime
	 *            1445946856
	 * @return 19:54
	 */
	public static String getTimeStampFormatWithH(String datetime) {

		try {
			Date date = new Date(Long.parseLong(datetime) * 1000);
			// System.out.println("------------" + datetime + "-----"
			// + date.toLocaleString());
			SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
			return sdf.format(date);

		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 日期格式字符串转换成时间戳
	 * @param date_str:字符串日期
	 * @return format 如：yyyy-MM-dd
	 */
	public static String date2TimeStamp(String date_str) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 时间格式可以变化，eg:
																		// yyyy-MM-dd
																		// HH:mm:ss
			return String.valueOf(sdf.parse(date_str).getTime() / 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	   /** 
	    * 获取过去第几天的日期 
	    * 
	    * @param past 
	    * @return 
	    */  
	   public static String getPastDate(int past) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	       String result = format.format(today);  
	       return result;  
	   }  
	  
	   /** 
	    * 获取未来 第 past 天的日期 
	    * @param past 
	    * @return 
	    */  
	   public static String getFetureDate(int past) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
	       String result = format.format(today);  
	       return result;  
	   }  
}
