/**
 * Toolkit.java
 * Copyright (C) 2010 SLMS
 * Simple Logistics Management System
 * 
 */
package com.zds.slms.util;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Copyright (C) 2010:SLMS<br/>
 * Project:SLMS<br/>
 * Module ID:<br/>
 * Comments:Simple Logistics Management System<br/>
 * JDK version used:JDK1.6<br/>
 * Package:com.zds.slms.util<br/>
 * Author:chenyongzheng<br/>
 * Create Date:2010-12-3<br/>
 * Modified By:<br/>
 * Modified Date:<br/>
 * Why & What is modified:<br/>
 * Version:0.1<br/>
 */
public class Toolkit {
	/**
	 * 获取当前日期是星期几
	 * 
	 * @param dt
	 * @return 当前日期是星期几
	 */
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);

		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;

		return weekDays[w];
	}

	/**
	 * 比较两个日期的大小
	 * 
	 * @param date1
	 * @param date2
	 * @return date2>date1:1 date2=date1 :0 date2<date1 :-1
	 */
	public static int compareDate(Date date1, Date date2) {
		return date2.compareTo(date1);
	}

	/**
	 * 将字符串按照一定格式转换成日期类型
	 * 
	 * @param source
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static Date convertStringToDate(String source, String pattern)
			throws Exception {
		if ((source == null) || (source.trim().length() == 0)) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(source);
	}

	/**
	 * 
	 * 将日期类型按照一定格式转换成字符串
	 * 
	 * @param date1
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static String convertDateToString(Date date1, String pattern)
			throws Exception {
		if (date1 == null) {
			return null;
		}
		// 时间格式设置；
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		return sdf.format(date1);
	}

	/**
	 * 获得系统当前时时间
	 * 
	 * @return
	 */
	public static Date getCurrTime() {
		return Calendar.getInstance().getTime();
	}

	/**
	 * 某个日期在某月中的最大日历
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMaxOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		Date minDate = getMinOfMonth(date);
		cal.setTime(minDate);
		// cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();

	}

	/**
	 * 某个日期在某月中的最小日历
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMinOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();

	}

	/**
	 * 传入两个整数计算其百分比保留两小数
	 * 
	 * @param num
	 * @param denom
	 * @return
	 */
	public static String getNumberPercentString(int num, int denom) {
		double dNum = num * 1.0;
		double dDenom = denom * 1.0;

		NumberFormat nf = NumberFormat.getPercentInstance();
		nf.setMinimumFractionDigits(2);
		return nf.format(dNum / dDenom);

	}

	/**
	 * 获取两个日期之间的工作日数
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDiffDaysNoWeekend(Date start, Date end) {
		int rtn = Integer.MIN_VALUE;

		// Total days
		int days = getDiffDaysWithWeekend(start, end);

		// Monday 1 -- Sunday 7
		int weekStart = getWeek(start);

		// Week count
		int intg = days / 7;

		// left days
		int mod1 = days % 7;

		// left days modified
		int mod2 = (weekStart + mod1) % 7;

		int factor = 0;
		if (mod1 != 0 && mod2 < weekStart) {

			// Start date is from Monday 1 -- Saturday 6.
			if (weekStart != 7) {
				factor = mod2 + 1;
				// Max value is 2
				if (factor > 2)
					factor = 2;
			} else {
				factor = 1;
			}
		}
		rtn = days - intg * 2 - factor;
		return rtn;
	}

	/**
	 * 两个日期间的间隔天数
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getDiffDays(Date start, Date end) {
		int rtn = Integer.MIN_VALUE;
		if (start != null && end != null) {
			long lngMinMilSec = start.getTime();
			long lngMaxMilSec = end.getTime();
			rtn = (int) ((lngMaxMilSec - lngMinMilSec) / (1000 * 60 * 60 * 24));
		}
		return rtn;
	}

	/**
	 * 两个日期间的间隔工作日
	 * 
	 * @param start
	 * @param end
	 * @return
	 */

	public static int getDiffDaysWithWeekend(Date start, Date end) {
		int rtn = getDiffDays(start, end);
		return rtn != Integer.MIN_VALUE ? rtn + 1 : Integer.MIN_VALUE;
	}

	/**
	 * 获取星期数
	 * 
	 * @param dt
	 * @return
	 */
	public static int getWeek(Date dt) {
		int rtn = Integer.MIN_VALUE;
		Calendar cd = Calendar.getInstance();
		cd.setTime(dt);
		int week = cd.get(Calendar.DAY_OF_WEEK);
		if (week == 1) {
			rtn = 7;
		} else {
			rtn = week - 1;
		}
		return rtn;
	}

}
