java 获得工作周数

package util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

public class WeekDay {
	private Date firstWeek;
	
	private WeekDay(){
		try {
			SimpleDateFormat year=new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat firstDay=new SimpleDateFormat("yyyy-01-01");
			firstWeek=year.parse(firstDay.format(new Date()));
			switch(firstWeek.getDay()){
				case 0:firstWeek=DateUtils.addDays(firstWeek,-6);break;//周末
				case 1:break;//周一
				case 2:firstWeek=DateUtils.addDays(firstWeek,-1);break;
				case 3:firstWeek=DateUtils.addDays(firstWeek,-2);break;
				case 4:firstWeek=DateUtils.addDays(firstWeek,-3);break;
				case 5:firstWeek=DateUtils.addDays(firstWeek,-4);break;
				case 6:firstWeek=DateUtils.addDays(firstWeek,-5);break;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	private WeekDay(Date firstDay){
		firstWeek=firstDay;
	}
	public long getWeekCount(){
		return getWeekCount(new Date());
	}
	public long getWeekCount(Date nowDate){
		SimpleDateFormat year=new SimpleDateFormat("yyyy-MM-dd");
		switch(nowDate.getDay()){
			case 0:break;//周末
			case 1:nowDate=DateUtils.addDays(nowDate,6);break;//周1
			case 2:nowDate=DateUtils.addDays(nowDate,5);break;
			case 3:nowDate=DateUtils.addDays(nowDate,4);break;
			case 4:nowDate=DateUtils.addDays(nowDate,3);break;
			case 5:nowDate=DateUtils.addDays(nowDate,2);break;
			case 6:nowDate=DateUtils.addDays(nowDate,1);break;
		}
		long dayCount=nowDate.getTime()-firstWeek.getTime();
		int minute=60*1000;
		return (dayCount/(24*60*minute))/7;
	}
}

 

你可能感兴趣的:(java)