SalaryTool.java 7.87 KB
Newer Older
ilal committed
1 2
package cn.timer.api.controller.xcgl;

ilal committed
3
import java.text.DecimalFormat;
ilal committed
4 5 6 7 8 9 10 11
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SalaryTool {
	static SimpleDateFormat mat = new SimpleDateFormat("yyyy-MM-dd");
ilal committed
12
	static SimpleDateFormat matM = new SimpleDateFormat("yyyy-MM");
ilal committed
13
	
ilal committed
14 15 16 17 18 19 20 21
	/**
     * 保留两位小数,四舍五入的一个老土的方法
     * @param d
     * @return
     */
    public static double formatDouble(double d) {
        return (double)Math.round(d*10000)/10000;
    }
ilal committed
22 23 24 25 26 27 28
    
    public static double formatDouble_(double d) {
    	double zhi = 0;
    	DecimalFormat df = new DecimalFormat("0.00");
    	zhi = Double.valueOf(df.format(d));
    	return zhi;
    }
ilal committed
29
	
ilal committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   /** 
    * 获取两个日期相差的月数 
    * @param d2  较大的日期 
    * @param d1  较小的日期 
    * @return 如果d1>d2返回 月数差 否则返回0 
    */ 
    public static int getMonthDiff(String d1, String d2)throws ParseException { 
        Calendar c1 = Calendar.getInstance(); 
        Calendar c2 = Calendar.getInstance();

        //将String日期转换成date
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
        java.util.Date date1=sdf.parse(d1); 
        java.util.Date date2=sdf.parse(d2); 
        c1.setTime(date1); 
        c2.setTime(date2);

       //判断两个日期的大小
        if(c2.getTimeInMillis() < c1.getTimeInMillis()) return 0; 
        int year1 = c1.get(Calendar.YEAR); 
        int year2 = c2.get(Calendar.YEAR); 
        int month1 = c1.get(Calendar.MONTH); 
        int month2 = c2.get(Calendar.MONTH); 
        int day1 = c1.get(Calendar.DAY_OF_MONTH); 
        int day2 = c2.get(Calendar.DAY_OF_MONTH); 
        // 获取年的差值 假设 d1 = 2015-9-30   d2 = 2015-12-16 
        int yearInterval = year2 - year1; 
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数 
        if(month2 < month1 || month1 == month2 && day2 < day1) yearInterval --; 
        // 获取月数差值 
        int monthInterval = (month2 + 12) - month1 ; 
        if(day2 > day1) monthInterval ++; 
        monthInterval %= 12; 
        return yearInterval * 12 + monthInterval + 1; 
    } 
	
ilal committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	
	// 上个月
		public static String getfirstlastTwo(String time) {
			String firstnext;
			Calendar c1 = Calendar.getInstance();
			c1.setTime(strToDateLong(time)); // 设置为当前时间
			c1.add(Calendar.MONTH, -1);
			c1.set(Calendar.DAY_OF_MONTH, 1);
			firstnext = mat.format(c1.getTime());
			return firstnext;
		}
		public static String getlastlastTwo(String time) {
			String lastnext;
			Calendar ca1 = Calendar.getInstance();
			ca1.setTime(strToDateLong(time)); // 设置为当前时间
			ca1.add(Calendar.MONTH, -1);
			ca1.set(Calendar.DAY_OF_MONTH, ca1.getActualMaximum(Calendar.DAY_OF_MONTH));
			lastnext = mat.format(ca1.getTime());
			return lastnext;
		}
		
		// 本月
		public static String getfirstnowTwo(String time) {
			String firstnow;
			Calendar c = Calendar.getInstance();
			c.setTime(strToDateLong(time)); // 设置为当前时间
			c.add(Calendar.MONTH, 0);
			c.set(Calendar.DAY_OF_MONTH, 1);
			firstnow = mat.format(c.getTime());
			return firstnow;
		}
		public static String getlastnowTwo(String time) {
			String lastnow;
			Calendar ca = Calendar.getInstance();
			ca.setTime(strToDateLong(time)); // 设置为当前时间
			ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
			lastnow = mat.format(ca.getTime());
			return lastnow;
		}
		
		// 下个月
		public static String getfirstnextTwo(String time) {
			String firstnext;
			Calendar c1 = Calendar.getInstance();
			c1.setTime(strToDateLong(time)); // 设置为当前时间
			c1.add(Calendar.MONTH, 1);
			c1.set(Calendar.DAY_OF_MONTH, 1);
			firstnext = mat.format(c1.getTime());
			return firstnext;
		}
		public static String getlastnextTwo(String time) {
			String lastnext;
			Calendar ca1 = Calendar.getInstance();
			ca1.setTime(strToDateLong(time)); // 设置为当前时间
			ca1.add(Calendar.MONTH, 1);
			ca1.set(Calendar.DAY_OF_MONTH, ca1.getActualMaximum(Calendar.DAY_OF_MONTH));
			lastnext = mat.format(ca1.getTime());
			return lastnext;
		}
		
		/**
	     * 	根据 年、月 获取对应的月份 的 天数
	     */
		 public static int getDaysByYearMonth(int year, int month)
		 {
		     Calendar a = Calendar.getInstance();
		     a.set(Calendar.YEAR, year);
		     a.set(Calendar.MONTH, month - 1);
		     a.set(Calendar.DATE, 1);
		     a.roll(Calendar.DATE, -1);
		     int maxDate = a.get(Calendar.DATE);
		     return maxDate;
		 }
		
		 /**
		 *	 获取月份起始日期
		 * 
		 * @param date
		 * @return
		 * @throws ParseException
		 */
		public static String getMinMonthDate(String date) throws ParseException {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(mat.parse(date));
			calendar.set(Calendar.DAY_OF_MONTH,
					calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
			return mat.format(calendar.getTime());
		}

		/**
		 *	 获取月份最后日期
		 * 
		 * @param date
		 * @return
		 * @throws ParseException
		 */
		public static String getMaxMonthDate(String date) throws ParseException {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(mat.parse(date));
			calendar.set(Calendar.DAY_OF_MONTH,
					calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			return mat.format(calendar.getTime());
		}
		
ilal committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
		
		// 下个月
		public static String getfirstnextMo(String time) {
			String firstnext;
			Calendar c1 = Calendar.getInstance();
			c1.setTime(strToDateLong(time)); // 设置为当前时间
			c1.add(Calendar.MONTH, 1);
			c1.set(Calendar.DAY_OF_MONTH, 1);
			firstnext = matM.format(c1.getTime());
			return firstnext;
		}
		
		// 上个月
		public static String getfirstlastMo(String time) {
			String firstnext;
			Calendar c1 = Calendar.getInstance();
			c1.setTime(strToDateLong(time)); // 设置为当前时间
			c1.add(Calendar.MONTH, -1);
			c1.set(Calendar.DAY_OF_MONTH, 1);
			firstnext = matM.format(c1.getTime());
			return firstnext;
		}
ilal committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	//##################################################################################################################################
	
	// 上个月
	public static String getfirstlast() {
		String firstnext;
		Calendar c1 = Calendar.getInstance();
		c1.add(Calendar.MONTH, -1);
		c1.set(Calendar.DAY_OF_MONTH, 1);
		firstnext = mat.format(c1.getTime());
		return firstnext;
	}

	public static String getlastlast() {
		String lastnext;
		Calendar ca1 = Calendar.getInstance();
		ca1.add(Calendar.MONTH, -1);
		ca1.set(Calendar.DAY_OF_MONTH, ca1.getActualMaximum(Calendar.DAY_OF_MONTH));
		lastnext = mat.format(ca1.getTime());
		return lastnext;
	}

	// 本月
	public static String getfirstnow() {
		String firstnow;
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);
		firstnow = mat.format(c.getTime());
		return firstnow;
	}

	public static String getlastnow() {
		String lastnow;
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		lastnow = mat.format(ca.getTime());
		return lastnow;
	}

	// 下个月
	public static String getfirstnext() {
		String firstnext;
		Calendar c1 = Calendar.getInstance();
		c1.add(Calendar.MONTH, 1);
		c1.set(Calendar.DAY_OF_MONTH, 1);
		firstnext = mat.format(c1.getTime());
		return firstnext;
	}

	public static String getlastnext() {
		String lastnext;
		Calendar ca1 = Calendar.getInstance();
		ca1.add(Calendar.MONTH, 1);
		ca1.set(Calendar.DAY_OF_MONTH, ca1.getActualMaximum(Calendar.DAY_OF_MONTH));
		lastnext = mat.format(ca1.getTime());
		return lastnext;
	}
}