package com.muchinfo.mtp3common.utils;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 日期时间操作工具类
 *
 */
public class DateTimeUtils {
    
    public static final Logger logger = LoggerFactory.getLogger(DateTimeUtils.class);
    
	public static final String YEAR_PATTERN = "yyyy";
	public static final String MONTH_PATTERN = "MM";
	public static final String DAY_PATTERN = "dd";
	public static final String DATE_CHAR_PATTERN = "yyyyMMdd";
	public static final String DATE_PATTERN = "yyyy-MM-dd";
	public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm";
	public static final String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String ORA_DATE_PATTERN = "yyyy-mm-dd";
	public static final String ORA_DATETIME_PATTERN = "yyyy-mm-dd hh24:mi:ss";
	public static final String ORA_TIMESTAMP_PATTERN = "yyyy-mm-dd hh24:mi:ss.ff3";
	public static final String ORA_HOUANDMIN_PATTERN = "HH:mm";
	public static final String DATE_CHAR_PATTERN_TIME = "yyyyMMddHHmmss";
	public static final String ORA_HOUANDMIN_IIME = "HHmmssSSS";
	public static final String ORA_PATTERN_TIME = "HH:mm:ss";

	public static final int SUNDAY = 0;
	public static final int MONDAY = 1;
	public static final int TUESDAY = 2;
	public static final int WEDNESDAY = 3;
	public static final int THURSDAY = 4;
	public static final int FRIDAY = 5;
	public static final int SATURDAY = 6;
	public static String format(Object date) {
		return format(date, null);
	}

	/**
	 * 返回日期的年周数（YYYYWW)，周数不足两位前面补0
	 * */
	public static String getYearWeekNum(String date){
		if (StringUtils.isBlank(date)){
			return "";
		}
		String datetime = date + " 00:00:00";
		Integer weekindex = DateTimeUtils.getWeekOfYear(datetime, DateTimeUtils.DATE_PATTERN);
		Integer year = DateTimeUtils.getNowYear(datetime, DateTimeUtils.DATETIME_PATTERN);
		// 周数不足两位前面补0
		String yearWeekNum = year.toString() + String.format("%02d", weekindex);

		return yearWeekNum;
	}
	
	/**
	 * 获取当前时间字符串
	 * @param format 日期格式
	 * @return
	 */
	public static String getDateTime(String format){ 
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(new Date());  
	}

	/**
	 * 格式化指定日期
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateTime(Date date,String format){ 
		if(date == null)
			return "";
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}


	/**
	 * 比较两个日期相差的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static double getDoubleMargin(String date1, String date2) {  
		double margin;
		SimpleDateFormat df = new SimpleDateFormat(DATETIME_PATTERN);
		ParsePosition pos = new ParsePosition(0);
		ParsePosition pos1 = new ParsePosition(0);
		Date dt1 = df.parse(date1, pos);
		Date dt2 = df.parse(date2, pos1);
		long l = dt1.getTime() - dt2.getTime();
		margin = (l / (24 * 60 * 60 * 1000.00));
		return margin;
	} 

	/**
	 * 比较两个日期相差的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static double getDoubleMargin(Date date1, Date date2) {  
		double margin;
		long l = date1.getTime() - date2.getTime();
		margin = (l / (24 * 60 * 60 * 1000.00));
		return margin;
	} 
	
	/**
	 * 比较两个日期相差的分钟
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static double getDoubleMin(Date date1, Date date2) {  
		double margin;
		long l = date1.getTime() - date2.getTime();
		margin = (l / (60 * 1000.00));
		return margin;
	} 
	
	public static double comparaHour(Date d1, Date d2) {
        long time1 = d1.getTime();
        long time2 = d2.getTime();
        double diff = time1 > time2 ? time1 - time2 : time2 - time1;
        double value = diff / (60 * 60 * 1000);
        return value;

    }

	/**
	 * 日期增加天数
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDay(Date date, int days) {  
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, cal.get(Calendar.DATE)+ days);
		return new Date(cal.getTimeInMillis());
	}
	
	public static Date addTime(Date date, int hour, int minute){
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date); 
		if(hour != 0) c.add(Calendar.HOUR, hour);
		if(minute != 0) c.add(Calendar.MINUTE, minute);
		return c.getTime();
	}

	public static String format(Object date, String pattern) {
		if (pattern == null)
			pattern = DATE_PATTERN;

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}

	public static String formatChar(Object date, String pattern) {
		if (pattern == null)
			pattern = DATE_CHAR_PATTERN;

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}

	public static String formatDatetime(Date date) {
		return formatDatetime(date, null);
	}

	public static String formatDatetime(Date date, String pattern) {
		if (pattern == null)
			pattern = DATETIME_PATTERN;

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}

	public static Date parseDate(String dateString)
			throws ParseException {
		return parseDate(dateString, null);
	}

	public static Date getNowParseDate(String pattern)throws ParseException{
		Date now = new Date();
		if(null == pattern){
			return new Date();
		}
		return parseDate(format(now,pattern),pattern);
	}

	public static Date parseDate(String dateString, String pattern)
			throws ParseException {
		if (pattern == null)
			pattern = DATE_PATTERN;

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.parse(dateString);
	}

	public static Date parseDatetime(String dateString)
			throws ParseException {
		return parseDatetime(dateString, null);
	}

	public static Date parseDatetime(String dateString, String pattern)
			throws ParseException {
		if (pattern == null)
			pattern = DATETIME_PATTERN;

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.parse(dateString);
	}

	public static Timestamp parseTimestamp(String dateString)
			throws ParseException {
		return parseTimestamp(dateString, null);
	}

	public static Timestamp parseTimestamp(String dateString, String pattern)
			throws ParseException {
		if (pattern == null)
			pattern = DATETIME_PATTERN;

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		Date date = simpleDateFormat.parse(dateString);
		return new Timestamp(date.getTime());
	}

	public static Date addDate(Date date, int month, int day) {
		if (date == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, month);
		c.add(Calendar.DAY_OF_MONTH, day);
		return c.getTime();
	}

	public static String addDate(String dateStr, int month, int day) throws ParseException {
		if (dateStr == null) {
			throw new IllegalArgumentException("The date must not be null");
		}
		Date date = parseDate(dateStr,DATE_CHAR_PATTERN);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, month);
		c.add(Calendar.DAY_OF_MONTH, day);
		return format(c.getTime(),DATE_CHAR_PATTERN);
	}

	public static int compareDate(Date fDate, Date sDate,String pattern) {
		String fStr = format(fDate, pattern);
		String sStr = format(sDate, pattern);
		if (sStr.equals(fStr)) {
			return 0;
		}
		return fStr.compareTo(sStr);
	}

	/**
	 * 验证时间格式，HH:mm:ss
	 * @param str
	 * @author wuxiaolin_001@126.com
	 * @Created 2012 2012-2-27下午04:05:37
	 */
	public static boolean isTime(String str){
		if(str == null) return false ;
		Pattern pattern = Pattern.compile("([0-9]|([0-1][0-9])|(2[0-3])):([0-9]|([0-5][0-9])):([0-9]|([0-5][0-9]))") ;
		return pattern.matcher(str.trim()).matches();
	}

	/**
	 * 获取当前系统时间的时间戳精确到秒
	 * @return
	 */
	public static String getNowTimeStamp(){
		Long time=System.currentTimeMillis();
		String nowTimeStamp=String.valueOf(time/1000);
		return nowTimeStamp;
	}

	/**
	 * 获取当前系统时间以HHmmss格式返回
	 * @ReturnType String
	 * @author wuxiaolin_001@126.com
	 * @Created 2012 2012-9-25下午04:13:58
	 */
	public static String getTime(){
		Calendar ca = Calendar.getInstance();
		int hour=ca.get(Calendar.HOUR_OF_DAY);//24小时制
		int minute=ca.get(Calendar.MINUTE);//分
		int second=ca.get(Calendar.SECOND);//秒
		return matchLenth(String.valueOf(hour),2)+matchLenth(String.valueOf(minute),2)+matchLenth(String.valueOf(second),2);
	}

	private static String matchLenth(String str,int len){
		if(str.length()<len){
			str="0"+str;
		}
		return str ;
	}

	public static Long dateDiff(String startTime, String endTime,   
			String format, String str) {   
		// 按照传入的格式生成一个simpledateformate对象   
		SimpleDateFormat sd = new SimpleDateFormat(format);   
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数   
		long nh = 1000 * 60 * 60;// 一小时的毫秒数   
		long nm = 1000 * 60;// 一分钟的毫秒数   
		long ns = 1000;// 一秒钟的毫秒数   
		long diff;   
		long day = 0;   
		long hour = 0;   
		long min = 0;   
		long sec = 0;
		long totalSec = 0;
		// 获得两个时间的毫秒时间差异   
		try {   
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();   
			day = diff / nd;// 计算差多少天   
			hour = diff % nd / nh + day * 24;// 计算差多少小时   
			min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟   
			sec = diff % nd % nh % nm / ns;// 计算差多少秒   

			totalSec = diff / ns ; //计算总共差多少秒
			// 输出结果   
			System.out.println("时间相差：" + day + "天" + (hour - day * 24) + "小时"  
					+ (min - day * 24 * 60) + "分钟" + sec + "秒。");

		} catch (ParseException e) {   
			e.printStackTrace();   
		}   
		if (str.equalsIgnoreCase("h")) {   
			return hour;   
		} else if (str.equalsIgnoreCase("s")) {   
            return sec;   
        }else if (str.equalsIgnoreCase("totalSec")) {   
            return totalSec;   
        }{   
			return min;   
		}   
	}

	public static Date getSysDatetime() throws ParseException{
		Date date = new Date();
		SimpleDateFormat sdf=new SimpleDateFormat(DATETIME_PATTERN);
		return parseDatetime(sdf.format(date));
	}

	public static Timestamp getSysTimestamp() {
		Date date = new Date();   
		Timestamp nousedate = new Timestamp(date.getTime());  
		return nousedate;
	}
	
	/**
	 * @Title: isFirstDayOfMonth 
	 * @Description: 判断给定时间是否是当月一号
	 */
	public static Boolean isFirstDayOfMonth(Date date){
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    if(calendar.get(Calendar.DAY_OF_MONTH) == 1){
	        return true;
	    }
        return false;
	}
	
	/**
	 * @Title: getDateNumber 
	 * @Description: 获取当前日期是对于的星期数字，0表示星期日，以此类推。。。
	 */
	public static Integer getWeekDayNumber(Date date){
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		logger.debug("new date:"+date);
		String weekname=new SimpleDateFormat("EEEE",Locale.CHINA).format(c.getTime());
		logger.debug("weekname:"+weekname);
		String[] weekdaynames={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
		Integer[] weekdaycodes={0,1,2,3,4,5,6};
		Integer weekcode=0;
		for(int i=0;i<weekdaynames.length;i++){
			if(weekdaynames[i].equals(weekname)){
				weekcode=weekdaycodes[i];
				logger.debug("for weekcode:"+weekcode);
				break;
			}
		}
		logger.debug("return weekcode:"+weekcode);
		return weekcode;
	}
	
	/**
	 * @Title: formatStr 
	 * @Description: 时间字符串类型转化为制定格式字符串
	 * @param @param 适用时间字符串为（2017-07-07 13:40:40 ，2017-07-07 13:40 等）
	 * @throws ParseException
	 */
	public static String formatStr(String str,String pattern){
	    Date date = null;
	    boolean flag = true;
	    int i = str.indexOf(" ");
	    if(i > 0){
	        String time = str.substring(i);
	        if(!isTime(time)){
	            time = time.concat(":00");
	            str = str.substring(0,i).concat(time);
	        }
	        flag = false;
	    }
	    try {
	        if(flag){
	            date=  parseDate(str);
	        }else{
	            date = parseDatetime(str); 
	        }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return getDateTime(date, pattern);
	}

	/**
	 * 判断两个时间段是否有重叠
	* @param beginDate1 时间段1 开始时间
	* @param endDate1 时间段1 结束时间
	* @param beginDate2 时间段2 开始时间
	* @param endDate2 时间段2 结束时间
	* @param pattern 时间格式
	* @return true 存在重叠
	* false 不重叠
	*/
    public static Boolean isOverLaps(String beginDate1, String endDate1,
            String beginDate2, String endDate2,String pattern) {
        if (pattern == null)
            pattern = DATETIME_PATTERN;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date1b = sdf.parse(beginDate1);
            Date date1e = sdf.parse(endDate1);
            Date date2b = sdf.parse(beginDate2);
            Date date2e = sdf.parse(endDate2);
            if (date2b.compareTo(date1e) * date2e.compareTo(date1b) > 0) {
                return false;
            } else {// 判断是否是时间交接点:08:00-12:00 12:00-15:00
                if (date2b.equals(date1e) || date2e.equals(date1b)) {
                    return false;
                } else{
                    return true; 
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 判断两个时间段是否有重叠(适用四个时间格式相同)
    * @param date1b 时间段1 开始时间
    * @param date1e 时间段1 结束时间
    * @param date2b 时间段2 开始时间
    * @param date2e 时间段2 结束时间
    * @return true 存在重叠   , false 不重叠
    */
    public static Boolean isOverLaps(Date date1b, Date date1e,
            Date date2b, Date date2e) {
        if (date2b.compareTo(date1e) * date2e.compareTo(date1b) > 0) {
            return false;
        } else {
            return true; 
        }
    }

	public static String getWeekDayName(int day){
		switch (day){
			case SUNDAY:return "星期日";
			case MONDAY:return "星期一";
			case TUESDAY:return "星期二";
			case WEDNESDAY:return "星期三";
			case THURSDAY:return "星期四";
			case FRIDAY:return "星期五";
			case SATURDAY:return "星期六";
		}
		return "";
	}
	
	/**
	 * 根据日期字符串获取周的开始时间
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static Date getBeginDayOfWeek(String datetime, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		try{
			Date date = null;
			if(datetime == null || datetime.equals(""))
				date = new Date();
			else
				date = sdf.parse(datetime);
			cal.setTime(date);
			int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
			if(dayofweek == 1){
				dayofweek += 7;
			}
			cal.add(Calendar.DATE, 2 - dayofweek);
		}catch(Exception px){
			px.printStackTrace();
		}
		return cal.getTime();
	}
	
	/**
	 * 根据日期字符串获取周的结时间
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static Date getEndDayOfWeek(String datetime, String format){
		Calendar cal = Calendar.getInstance();
		cal.setTime(getBeginDayOfWeek(datetime, format));
		cal.add(Calendar.DAY_OF_WEEK, 6);
		Date weekEndDate = cal.getTime();
		return weekEndDate;
	}
	
	/**
	 * 根据日期字符串获取年
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static int getNowYear(String datetime, String format){
		Date date = null;
		int year = 0;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try{
			if(datetime == null || datetime.equals(""))
				date = new Date();
			else
				date = sdf.parse(datetime);
			GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
			gc.setTime(date);
			year = gc.get(1);
		}catch(Exception px){
			px.printStackTrace();
		}
		
		return year;
	}
	
	/**
	 * 根据日期字符串获取月
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static int getNowMonth(String datetime, String format){
		Date date = null;
		int month = 1;
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try{
			if(datetime == null || datetime.equals(""))
				date = new Date();
			else
				date = sdf.parse(datetime);
			GregorianCalendar gc = (GregorianCalendar)Calendar.getInstance();
			gc.setTime(date);
			month = gc.get(2) + 1;
		}catch(Exception px){
			px.printStackTrace();
		}
		
		return month;
	}
	
	/**
	 * 根据日期获取当月的第一天
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static Date getBeginDayOfMonth(String datetime, String format){
		Calendar calendar = Calendar.getInstance();
		calendar.set(getNowYear(datetime, format), getNowMonth(datetime, format) - 1, 1);
		return calendar.getTime();
	}
	
	/**
	 * 根据日期获取当月的最后一天
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static Date getEndDayOfMonth(String datetime, String format){
		Calendar calendar = Calendar.getInstance();
		calendar.set(getNowYear(datetime, format), getNowMonth(datetime, format) - 1, 1);
		int day = calendar.getActualMaximum(5);
		calendar.set(getNowYear(datetime, format), getNowMonth(datetime, format) - 1, day);
		return calendar.getTime();
	}
	
	/**
	 * 根据日期获取在全年的第几周
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static int getWeekOfYear(String datetime, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		int week = 0;
		try{
			Date date = sdf.parse(datetime);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			week = calendar.get(Calendar.WEEK_OF_YEAR);
		}catch(Exception px){
			px.printStackTrace();
		}
		return week;
	}
	
	/**
	 * 根据每周几或每月几号计算下次触发时间
	 * @param type 方式 1:每周 2:每月
	 * @param dayOrWeek 周几（星期日:0~星期六:6）或几号
	 * @return
	 */
	public static Date getDate(int type, int dayOrWeek){
		Date date = null;
		Calendar c = Calendar.getInstance();
		int day = c.get(Calendar.DAY_OF_MONTH);
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		try {
			if(type == 1){
				dayOrWeek += 1;
				if(dayOrWeek < dayOfWeek){
					c.add(Calendar.DAY_OF_WEEK, 7);
				}
				c.set(Calendar.DAY_OF_WEEK, dayOrWeek);
			}else{
				if(dayOrWeek < day){
					c.add(Calendar.MONTH, 1);
				}
				c.set(Calendar.DAY_OF_MONTH, dayOrWeek);
			}
//			System.out.println(c.getTime());
			date = parseDate(getDateTime(c.getTime(),DATE_PATTERN));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return date;
	}
	
	/**
	 * 是否到期 true到期，false未到期
	 * @param datetime
	 * @param format
	 * @return
	 */
	public static Boolean compare(String datetime, String format){
		Boolean flag = true;
		try {
			if (format == null)
				format = DATE_PATTERN;
			Date sysdate = new Date();		
			String datetime2 = getDateTime(sysdate, format);
			Date date = parseDate(datetime, format);
			Date date2 = parseDate(datetime2, format);
			if(date2.after(date)){
				flag =  true;
			}else{
				flag =  false;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public  static String lastYear(String year,int yearnum) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat(YEAR_PATTERN);
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, Integer.parseInt(year));
		c.add(Calendar.YEAR, yearnum);
		Date d = c.getTime();
		
		return sdf.format(d);
	}
	
	public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }
	
	public static Map<Integer,Object> getDateMap(String startdate,String enddate,String weekenddate){
		List<String> list = getDays(enddate, weekenddate);
	    Map<Integer,Object> map = new 	HashMap<Integer,Object>();
	    map.put(1,startdate+"-"+enddate);	
	    String firsedate = "";
	    for(int i = 1;i < list.size();i++){
	    	if(list.size()<=7&&map.size()==1){
	    		map.put(map.size()+1,list.get(0)+"-"+list.get(list.size()-1));	
	    		break;
	    	}else{
	    		if(StringUtils.isBlank(firsedate)){
	    			firsedate = list.get(i);
	    			if(list.size()-i<=7){
	    				map.put(map.size()+1, firsedate+"-"+list.get(list.size()-1));
	    				break;
	    			}
	    		}else{
	    			if(i%7==0){
	    				map.put(map.size()+1, firsedate+"-"+list.get(i));
	    				firsedate = "";
	    			}
	    		}
	    	}
	    	
	    }
	    
		
		
		
		return map;
	}
	
	/** * 获取某年第一天日期 * @param year 年份 * @return Date
	*/
	public static Date getYearFirst(int year){
			Calendar calendar = Calendar.getInstance();
			calendar.clear();
			calendar.set(Calendar.YEAR, year);
			Date currYearFirst = calendar.getTime();
		return currYearFirst;
	}
	
		/**
		 * 获取某年最后一天日期
		 * @param year 年份
		 * @return Date
	     */
	    public static Date getYearLast(int year){
		        Calendar calendar = Calendar.getInstance();
		        calendar.clear();
		        calendar.set(Calendar.YEAR, year);
		        calendar.roll(Calendar.DAY_OF_YEAR, -1);
		        Date currYearLast = calendar.getTime();
			return currYearLast;
	    }
	    
	    
	    public static Date getNextDate(Date date){
	    	  
	    	Calendar calendar = new GregorianCalendar();
	    	calendar.setTime(date);
	    	calendar.add(Calendar.DATE, 1);
	    	date = calendar.getTime();
	    	
	    	return date;
	    }
	    
	    
	    public static int getWeekDays(String datestr,String formatstr) throws ParseException{
	    	
	    	SimpleDateFormat formater = new SimpleDateFormat(formatstr);
	    	Date date = formater.parse(datestr);
	    	Calendar c = Calendar.getInstance();
	    	c.setTime(date);
	    	int dayForWeek = 0; 
	    	if(c.get(Calendar.DAY_OF_WEEK) == 1){ 
	    	dayForWeek = 7; 
	    	}else{ 
	    	dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; 
	    	} 
	    	return dayForWeek;
	    }
}
