package com.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhongdao.jlr.enums.Week;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class UtilTools {
	private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
	private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
	private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
	private static final String bxcompanykey = "2016052210383737344";// 太保上海分公司编号
	private static final String sysrolekey = "20150807152252189885";// 管理员角色编号
	private static final String adminCode = "2015050608332656789";// 超级管理员(admin)主键编号

	public static String getCode(String codeStr) {
		String str = "";
		if (codeStr.equals("bxcompanykey")) {
			str = bxcompanykey;
		} else if (codeStr.equals("sysrolekey")) {
			str = sysrolekey;
		} else if (codeStr.equals("adminCode")) {
			str = adminCode;
		}
		return str;
	}

	/**
	 * 获取List长度
	 * 
	 * @param List
	 * @return int
	 * 
	 */
	public static Integer ListCount(List list) {
		Integer count = new Integer(0);
		if (list != null && list.size() > 0) {
			count = list.size();
		}
		return count;
	}

	/**
	 * 得到当前日期 gs(格式):自定义
	 */
	public static String getTime(String gs) {
		SimpleDateFormat df = new SimpleDateFormat(gs);
		String currentdate = df.format(new Date());
		return currentdate;
	}

	/**
	 * 获取具体时间精确到日
	 */
	public static String getTimeForD() {
		return getTime("yyyy-MM-dd");
	}

	/**
	 * 获取具体时间精确到日
	 */
	public static String getTimeForD(String format) {
		return getTime(format);
	}

	/**
	 * 获取具体时间精确到时
	 */
	public static String getTimeForH() {
		return getTime("yyyy-MM-dd HH");
	}

	/**
	 * 获取具体时间精确到分
	 */
	public static String getTimeForM() {
		return getTime("yyyy-MM-dd HH:mm");
	}

	/**
	 * 获取具体时间精确到秒
	 */
	public static String getTimeForS() {
		return getTime("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 删除HTML标签
	 * 
	 * @param htmlStr
	 * @return
	 */
	public static String delHTMLTag(String htmlStr) {
		Pattern p_script = Pattern.compile(regEx_script,
				Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签

		Pattern p_style = Pattern
				.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签

		Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll(""); // 过滤html标签

		htmlStr = htmlStr.replaceAll("&nbsp;", " ");// 过滤空格标签

		return htmlStr.trim(); // 返回文本字符串
	}

	/**
	 * Date转String gs:时间格式
	 */
	public static String dateToString(Date date, String gs) {
		String result = "";
		SimpleDateFormat df = new SimpleDateFormat(gs);
		result = df.format(date);
		return result;
	}

	/**
	 * String转Date sj:时间,默认"yyyy-MM-dd" gs:时间格式
	 */
	public static Date stringToDate(String sj, String gs) {
		Date date = null;
		if (!strNotNull(sj)) {
			sj = getTimeForD();
		}
		SimpleDateFormat df = new SimpleDateFormat(gs);
		try {
			date = df.parse(sj);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 判断字符串是否为null或空
	 */
	public static boolean strNotNull(String str) {
		if (null != str && !"".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean strNotNullAndNullStr(String str) {
		if (null != str && !"".equals(str.trim()) && !"null".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 自定义添加Day获取新的日期 yyyy-MM-dd
	 * 
	 * @param args
	 */
	public static String getNewDateForDay(Integer day) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String rq = getTimeForD();
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(sdf.parse(rq));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.DAY_OF_MONTH, day);
		rq = sdf.format(calendar.getTime());
		return rq;
	}

	/**
	 * list转换为jsonArray
	 * 
	 * @param list
	 * @param names
	 * @return
	 */
	public static JSONArray listTojsonArray(List list, String[] names) {
		JSONArray jarry = new JSONArray();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				JSONObject object = new JSONObject();
				for (int j = 0; j < names.length; j++) {
					object.put(names[j],
							null == obj[j] ? "" : obj[j].toString());
				}
				jarry.add(object);
			}
		}
		return jarry;
	}

	/**
	 * list转换为jsonObject
	 *
	 * @param list
	 * @param names
	 * @return
	 */
	public static JSONObject listTojsonObject(List list, String[] names) {
		JSONObject object= new JSONObject();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				object = new JSONObject();
				for (int j = 0; j < names.length; j++) {
					object.put(names[j],
							null == obj[j] ? "" : obj[j].toString());
				}
			}
		}
		return object;
	}
	/**
	 * 返回新的字符串格式 'p1','p2',.....
	 * 
	 * @param str
	 *            要转换的字符串
	 * @param separator
	 *            字符串之间的分隔符 ,值为null时，默认为','
	 * @return
	 */
	public static String strToNewStr(String str, String separator) {
		String result = "";
		if (null != str && !"".equals(str)) {
			if (null == separator)
				separator = ",";
			String[] nstr = str.split(separator);
			for (int i = 0; i < nstr.length; i++) {
				if (!"".equals(result)) {
					result += ",";
				}
				result += "'" + nstr[i] + "'";
			}
		}
		return result;
	}

	/**
	 * 判断字符串是否是数字 true:数字 false:非数字
	 */
	public static boolean isNumber(String str) {
		if (null != str
				&& !"".equals(str.trim())
				&& str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 把Vo 封装成jsonObj
	 * 
	 * @param v
	 * @param thistablesx
	 * @return
	 */
	public static SetJsonObject voToJsonObj(Vo v, String[] thistablesx) {
		long startTime=System.currentTimeMillis();
		JSONArray json = new JSONArray();
		SetJsonObject jsonObj = new SetJsonObject();
		try {
			for (int i = 0; i < v.getRows().size(); i++) {
				Object[] obj = (Object[]) v.getRows().get(i);
				JSONObject jobj = new JSONObject();
				for (int j = 0; j < thistablesx.length; j++) {
					String strarr = null == obj[j] ? "" : obj[j].toString();
					String str = thistablesx[j];
					jobj.put(str, strarr);
				}
				json.add(jobj);
			}
			jsonObj.setRows(json);
			jsonObj.setTotal(v.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("voToJsonObjTime:"+(System.currentTimeMillis()-startTime));
		return jsonObj;
	}

	public static SetJsonObject voToJsonObj(Vo v) {
		SetJsonObject jsonObj = new SetJsonObject();
		jsonObj.setRows(v.getRows());
		jsonObj.setTotal(v.getTotal());
		return jsonObj;
	}

	/**
	 * 把List 封装成jsonObj
	 * 
	 * @param v
	 * @param thistablesx
	 * @return
	 */
	public static SetJsonObject listToJsonObj(List list, String[] thistablesx) {
		JSONArray json = new JSONArray();
		SetJsonObject jsonObj = new SetJsonObject();
		try {
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				JSONObject jobj = new JSONObject();
				for (int j = 0; j < thistablesx.length; j++) {
					String strarr = null == obj[j] ? "" : obj[j].toString();
					String str = thistablesx[j];
					jobj.put(str, strarr);
				}
				json.add(jobj);
			}
			jsonObj.setRows(json);
			jsonObj.setTotal(list.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObj;
	}
	/**
	 * 把List 封装成jsonObj
	 * 
	 * @param v
	 * @param thistablesx
	 * @return
	 */
	public static JSONArray listToJsonObj2(List list, String[] thistablesx) {
		JSONArray json = new JSONArray();
		try {
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				JSONObject jobj = new JSONObject();
				for (int j = 0; j < thistablesx.length; j++) {
					String strarr = null == obj[j] ? "" : obj[j].toString();
					String str = thistablesx[j];
					jobj.put(str, strarr);
				}
				json.add(jobj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	/**
	 * 得到前天 昨天 今天 明天的日期 -1代表前一天 1代表后一天 等等 默认 yyyy-MM-dd
	 */
	public static String getTcTime(String style, int numb) {
		if (!UtilTools.strNotNull(style)) {
			style = "yyyy-MM-dd";
		}
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, numb);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		SimpleDateFormat formatter = new SimpleDateFormat(style);
		String dateString = formatter.format(date);
		return dateString;
	}

	/**
	 * 得到上一个月第一天
	 */
	public static String getMMTop() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		String time = format.format(c.getTime());
		return time + "-01";
	}

	/**
	 * 得到上一个月最后一天
	 */
	public static String getMMNext() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		int MaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 按你的要求设置时间
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), MaxDay, 23, 59, 59);
		// 按格式输出
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String gtime = sdf.format(c.getTime()); // 上月最后一天
		return gtime;
	}

	/**
	 * 得到page
	 * 
	 * @return mysql默认0 oracle默认1
	 */
	public static Integer getPage(HttpServletRequest request) {
		Integer page = 1;
		if (null != request && null != request.getParameter("page")
				&& isNumber(request.getParameter("page"))) {
			return Integer.parseInt(request.getParameter("page"));
		}
		return page;
	}

	/**
	 * 得到rows
	 * 
	 * @return 默认10
	 */
	public static Integer getRows(HttpServletRequest request) {
		Integer rows = 10;
		if (null != request && null != request.getParameter("rows")
				&& isNumber(request.getParameter("rows"))) {
			return Integer.parseInt(request.getParameter("rows"));
		}
		return rows;
	}

	/**
	 * 字符串首字母转换大写
	 */
	public static String szmZdx(String result) {
		String result1 = "";
		if (null != result && !"".equals(result)) {
			result1 = result.substring(0, 1).toUpperCase()
					+ result.substring(1, result.length());
		}
		return result1;
	}

	/**
	 * 获取服务器地址：如：http://192.168.1.100:8080/web
	 * 
	 * @param request
	 * @return
	 */
	public static String getWebAddress(HttpServletRequest request) {
		String web1 = request.getServerName();
		// 把localhost替换成本机ip
		if (web1.equals("localhost")) {
			try {
				web1 = InetAddress.getLocalHost().getHostAddress();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		String path = "http://" + web1 + ":" + request.getServerPort()
				+ request.getContextPath();
		return path;
	}

	/**
	 * 判断手机号码是否正确
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern
				.compile("^(13[0-9]|15[012356789]|17[36780]|18[0-9]|14[57])[0-9]{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	/**
	 * 获取状态
	 */
	public static String getOrderState(Integer state) {
		String str = "初始值";
		if (0 == state) {
			str = "初始值";
		} else if (5 == state) {
			str = "待取车预约";
		} else if (10 == state) {
			str = "取车预约失败";
		} else if (15 == state) {
			str = "待取车派单";
		} else if (20 == state) {
			str = "待取车邀约";
		} else if (30 == state) {
			str = "取车邀约失败";
		} else if (31 == state) {
			str = "待取车";
		} else if (32 == state) {
			str = "取车条件不符";
		} else if (33 == state) {
			str = "待送修";
		} else if (34 == state) {
			str = "取车失败";
		} else if (343 == state) {
			str = "等待定损";
		}else if (345 == state) {
			str = "还车预约";
		}else if (347 == state) {
			str = "还车派单";
		}else if (35 == state) {
			str = "待还车邀约";
		} else if (351 == state) {
			str = "邀约失败重新";
		}else if (352 == state) {
			str = "邀约失败重新派单";
		}else if (36 == state) {
			str = "待维修网点取车";
		}else if (361 == state) {
			str = "提车失败";
		}  else if (37 == state) {
			str = "待还车";
		}else if (371 == state) {
			str = "还车失败";
		}  else if (40 == state) {
			str = "待理赔";
		} else if (100 == state) {
			str = "理赔完毕";
		} else if (101 == state) {
			str = "强制废除";
		}
		return str;
	}

	/**
	 * 获取状态
	 */
	public static String getOrderState(String state) {
		StringBuffer str = new StringBuffer();
		if ("初始值".contains(state)) {
			str.append("0,");
		} else if ("待取车预约".equals(state)) {
			str.append("5,");
		} else if ("取车预约失败".equals(state)) {
			str.append("10,");
		} else if ("待取车派单".equals(state)) {
			str.append("15,");
		} else if ("待取车邀约".equals(state)) {
			str.append("20,");
		} else if ("取车邀约成功".equals(state)) {
			str.append("25,");
		} else if ("取车邀约失败".equals(state)) {
			str.append("30,");
		}  else if ("待取车".equals(state)) {
			str.append("31,");
		} else if ("待送修".equals(state)) {
			str.append("33,");
		} else if ("取车失败".equals(state)) {
			str.append("34,");
		} else if ("等待定损".equals(state)) {
			str.append("343,");
		} else if ("还车预约".equals(state)) {
			str.append("345,");
		} else if ("还车派单".equals(state)) {
			str.append("347,");
		}
		else if ("待还车邀约".equals(state)) {
			str.append("35,");
		} 
		else if ("邀约失败重新预约".equals(state)) {
			str.append("351,");
		} else if ("邀约失败重新派单".equals(state)) {
			str.append("352,");
		} 
		else if ("待维修网点取车".equals(state)) {
			str.append("36,");
		} else if ("提车失败".equals(state)) {
			str.append("361,");
		} else if ("待还车".equals(state)) {
			str.append("37,");
		} else if ("还车失败 ".equals(state)) {
			str.append("371,");
		} else if ("还车成功待理赔".equals(state)) {
			str.append("40,");
		} else if ("理赔完毕".equals(state)) {
			str.append("100,");
		} else if ("强制废除".equals(state)) {
			str.append("101,");
		}
		return str.toString();
	}
	/**
	 * 获取状态App
	 */
	public static String getOrderStateApp(String state) {
		StringBuffer str = new StringBuffer();
		if ("初始值".contains(state)) {
			str.append("0,");
		} else if ("取车预约".equals(state)) {
			str.append("5,");
		} else if ("预约失败".equals(state)) {
			str.append("10,");
		} else if ("取车派单".equals(state)) {
			str.append("15,");
		} else if ("取车邀约".equals(state)) {
			str.append("20,");
		} else if ("邀约失败".equals(state)) {
			str.append("30,");
		} else if ("取车".equals(state)) {
			str.append("31,");
		} else if ("送修".equals(state)) {
			str.append("33,");
		} else if ("取车失败".equals(state)) {
			str.append("34,");
		} else if ("定损".equals(state)) {
			str.append("343,");
		} else if ("还车预约".equals(state)) {
			str.append("345,");
		} else if ("还车派单".equals(state)) {
			str.append("347,");
		}
		else if ("还车邀约".equals(state)) {
			str.append("35,");
		} 
		else if ("重新预约".equals(state)) {
			str.append("351,");
		} else if ("重新派单".equals(state)) {
			str.append("352,");
		} 
		else if ("提车".equals(state)) {
			str.append("36,");
		} else if ("提车失败".equals(state)) {
			str.append("361,");
		} else if ("还车".equals(state)) {
			str.append("37,");
		} else if ("还车失败 ".equals(state)) {
			str.append("371,");
		} else if ("理赔".equals(state)) {
			str.append("40,");
		} else if ("理赔完毕".equals(state)) {
			str.append("100,");
		} else if ("强制废除".equals(state)) {
			str.append("101,");
		}
		return str.toString();
	}

	/**
	 * 获取状态4SApp
	 */
	public static String get4SOrderStateApp(String state) {
		StringBuffer str = new StringBuffer();
		if ("等待送修".contains(state)) {
			str.append("5,");
		} else if ("送修途中".equals(state)) {
			str.append("10,");
		} else if ("返修途中".equals(state)) {
			str.append("20,");
		} else if ("接车成功".equals(state)) {
			str.append("30,");
		} else if ("交车失败".equals(state)) {
			str.append("35,");
		} else if ("开始维修".equals(state)) {
			str.append("40,");
		} else if ("维修完毕".equals(state)) {
			str.append("50,");
		} else if ("等待交车".equals(state)) {
			str.append("60,");
		} else if ("交车成功".equals(state)) {
			str.append("70,");
		} else if ("接车失败".equals(state)) {
			str.append("15,");
		} else if ("结账完毕".equals(state)) {
			str.append("100,");
		} else if ("强制撤销".equals(state)) {
			str.append("101,");
		}
		return str.toString();
	}

	/**
	 * 获取组号
	 */
	public static int getModenoByFiletype(int filetype) {
		if (filetype == 6 || filetype == 7 || filetype == 101) {
			return 0;
		} else if (filetype == 8) {
			return 1;
		} else if (filetype >= 9 && filetype <= 12) {
			return 2;
		} else if (filetype == 13) {
			return 3;
		} else if (filetype >= 14 && filetype <= 17) {
			return 4;
		} else if (filetype == 18) {
			return 5;
		} else if (filetype >= 19 && filetype <= 22) {
			return 6;
		} else if (filetype == 23) {
			return 7;
		} else if (filetype >= 24 && filetype <= 27) {
			return 8;
		} else if (filetype == 28) {
			return 9;
		} else if (filetype >= 29 && filetype <= 30) {
			return 10;
		} else if (filetype == 31) {
			return 11;
		}
		return -1;
	}

	/**
	 * 客户类型
	 */
	public static String getOrderCustomlb(String state) {
		if ("A类".equals(state)) {
			return "A";
		} else if ("B类".equals(state)) {
			return "B";
		}
		return "";
	}

	/**
	 * 下载
	 */
	public static boolean downFile(HttpServletResponse response, String path) {
		boolean flag = false;
		File file = null;
		try {
			file = new File(path);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (null != file) {
			if (file.exists()) {
				try {
					String filename = file.getName();
					// 弹出下载对话框
					response.reset();
					response.setContentType("application/octet-stream");
					filename = new String(filename.getBytes(System
							.getProperty("file.encoding")), "ISO-8859-1");
					response.addHeader("Content-Disposition",
							"attachment;filename=\"" + filename + "\"");
					OutputStream os = os = response.getOutputStream();
					InputStream is = is = new FileInputStream(file);
					BufferedInputStream bis = new BufferedInputStream(is);
					BufferedOutputStream bos = new BufferedOutputStream(os);
					// 用输入流进行先读，然后用输出流去写
					int bytesRead = 0;
					byte[] buffer = new byte[8192];
					while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
						bos.write(buffer, 0, bytesRead);
					}
					bos.flush();
					bos.close();
					bis.close();
					os.flush();
					os.close();
					is.close();
					flag = true;
				} catch (IOException e) {
					flag = false;
					e.printStackTrace();
				}
			}
		}
		return flag;
	}

	public static String parsJsonString(org.json.JSONObject json, String params) {
		try {
			return json.getString(params);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}

	/**
	 * 根据一个日期，返回是星期几的字符串
	 * 
	 * @param sdate
	 * @return
	 */
	// public static String getWeek(String sdate) {
	// // 再转换为时间
	// Date date =strToDate(sdate);
	// Calendar c = Calendar.getInstance();
	// c.setTime(date);
	// // int hour=c.get(Calendar.DAY_OF_WEEK);
	// // hour中存的就是星期几了，其范围 1~7
	// // 1=星期日 7=星期六，其他类推
	// return new SimpleDateFormat("EEEE").format(c.getTime());
	// }
	public static Week getWeekStr(String sdate) {
		int year = Integer.parseInt(sdate.substring(0, 4));
		int month = Integer.parseInt(sdate.substring(5, 7));
		int day = Integer.parseInt(sdate.substring(8, 10));

		Calendar c = Calendar.getInstance();

		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, day);

		String str = c.get(Calendar.DAY_OF_WEEK) + "";

		if ("1".equals(str)) {
			return Week.SUN;
		} else if ("2".equals(str)) {
			return Week.MON;
		} else if ("3".equals(str)) {
			return Week.TUE;
		} else if ("4".equals(str)) {
			return Week.WEN;
		} else if ("5".equals(str)) {
			return Week.THU;
		} else if ("6".equals(str)) {
			return Week.FRI;
		} else if ("7".equals(str)) {
			return Week.SAT;
		}
		return null;
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 字符串的日期格式的计算
	 */
	public static int daysBetween(String smdate, String bdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}
	/**
	 * 获得指定日期的前一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedDayBefore(String specifiedDay) {
		// SimpleDateFormat simpleDateFormat = new
		// SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c
				.getTime());
		return dayBefore;
	}

	/**
	 * 获得指定日期的后一天
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getSpecifiedDayAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd")
				.format(c.getTime());
		return dayAfter;
	}

}
