package com.huimv.modular.utils.util;

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

/**
 * 日期转换工具
 * @author guojian
 */
public class DateUtilsPublic {

	public static final String DATE_FORMAT = "yyyy-MM-dd";

	public static final String DATE_FORMAT_NUMBER = "yyyyMMdd";
	public static final String DATE_FORMAT_NUMBER_MONTH = "yyyyMM";

	public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String TIME_FORMAT = "HH:mm:ss";

	public static final String GREENWICH_TIME = "1970-01-01 00:00:00";

	public static String formatDate(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			return sdf.format(date);
		} catch (Exception e) {
			return null;
		}
	}






//改一下，传进来date  处理完之后返回date
	public static String addDateStringHour(String day, int hour) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		if (date == null)
			return "";
		System.out.println("front:" + format.format(date)); //显示输入的日期
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hour);// 24小时制
		date = cal.getTime();
		System.out.println("after:" + format.format(date));  //显示更新后的日期
		cal = null;
		return format.format(date);


	}



	//改一下，传进来date  处理完之后返回date
	public static Date addDateHour(Date datein, int hour) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		/*try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}*/
		//System.out.println("front:" +format.format(datein)); //显示输入的日期
		Calendar cal = Calendar.getInstance();
		cal.setTime(datein);
		cal.add(Calendar.HOUR, hour);// 24小时制
		date = cal.getTime();
		//System.out.println("after:" + format.format(date));  //显示更新后的日期
		cal = null;
		return  date ;
	}



	//改一下，传进来date  处理完之后返回date
	public static Date addDateMinute(Date datein, int minute) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		/*try {
			date = format.parse(day);
		} catch (Exception ex) {
			ex.printStackTrace();
		}*/
		//System.out.println("front:" +format.format(datein)); //显示输入的日期
		Calendar cal = Calendar.getInstance();
		cal.setTime(datein);
		cal.add(Calendar.MINUTE,minute );// 24小时制
		date = cal.getTime();
		//System.out.println("after:" + format.format(date));  //显示更新后的日期
		cal = null;
		return  date ;
	}




	public static void main(String[] args) {

		//int   a = Integer.parseInt(String .format("%td", new Date()));
		Date date = DateUtilsPublic.addDateMinute(new Date(), 250);
         //250分钟  四小时，，对的！
		System.out.println(date.toString());

	}









	//过去五秒
	public static Date getPassedsecond(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.SECOND, - 5);
		return c.getTime();
	}

	public static String stampToTime(String s) throws Exception{
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long lt = new Long(s);
		//将时间戳转换为时间
		Date date = new Date(lt);
       //将时间调整为yyyy-MM-dd HH:mm:ss时间样式
		res = simpleDateFormat.format(date);
		return res;

	}


	//过去一天
	public static Date getPassedDay(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.HOUR, - 1);
		return c.getTime();
	}


	//过去七天
	public static Date getPassedWeek(){
		SimpleDateFormat format = new SimpleDateFormat(DATETIME_FORMAT);
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, - 7);
		return c.getTime();
	}

	//过去三十天
	public static Date getPassedMonth(){
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, - 30);
		return c.getTime();
	}

	//指定时间的周一
	public static String getWeekFirstStr(Date time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
		return sdf.format(getThisWeekMonday(time));
	}

	//获取指定日期当月的第一天
	public static String getMonthFirstStr(Date time){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 0);
		return sdf.format(calendar.getTime());
	}

	public static Date geLastWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, -7);
		return cal.getTime();
	}


	public static String dateToString( Date datetime) {
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String dateString = formatter.format(datetime);
      return dateString;
}

	public static Date getNextWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday(date));
		cal.add(Calendar.DATE, 7);
		return cal.getTime();
	}

	public static Date getThisWeekMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}

	public static String getNowWithDigital() {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(now).replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
	}

	public static String buildDateTimeString(String formatContent) {
		String year = formatContent.substring(0, 4);
		String month = formatContent.substring(4, 6);
		String day = formatContent.substring(6, 8);
		String hour = formatContent.substring(8, 10);
		String minute = formatContent.substring(10, 12);
		String seconds = formatContent.substring(12, 14);
		StringBuilder dateTimeSb = new StringBuilder();
		return dateTimeSb.append(year).append("-").append(month).append("-").append(day).append(" ").append(hour).append(":").append(minute).append(":").append(seconds).toString();
	}

	public static String buildDateString(String formatContent) {
		String year = formatContent.substring(0, 4);
		String month = formatContent.substring(4, 6);
		String day = formatContent.substring(6, 8);
		String dateTime = year + "-" + month + "-" + day;
		return dateTime;
	}

	public static String formatDateTime(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
		return sdf.format(date);
	}

	public static Date parseDateTime(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(date);
	}


	public String getToday() {
		DateFormat df3 = DateFormat.getDateInstance(2, Locale.CHINA);
		return df3.format(new Date());
	}

	public String formatDate(Date date) {
		DateFormat df3 = DateFormat.getDateInstance(2, Locale.CHINA);
		return df3.format(date);
	}


	public String beforeTime(int times) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.set(11, calendar.get(11) - times);
		String endTime = sdf.format(calendar.getTime());
		System.out.println("提前3小时：" + endTime);
		return endTime;
	}


	//判断日是否相同
	public   static  Boolean  dayIsSame( Date date1 , Date  date2) {
		int   a = Integer.parseInt(String .format("%td", date1));
		int   b = Integer.parseInt(String .format("%td", date2));
		Boolean result = a==b;
		return result;
	}
}
