1. 程式人生 > >加班時間計算

加班時間計算

計算加班時間:

網上搜了一會沒有好的例子:

加班規則: 一小時之內不算,

1<=x<1.5小時按1算

1.5<=x<2小時按1.5算

2<=x<2.5小時按2算

有倆種思路:

一種是:按照半小時為單位,進行計算後再轉換為小時

另一種:取得整數對小數點後面的進行判斷

(1)

package com.thinkgem.jeesite.modules.kaoqin.entrawork.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.kaoqin.entrawork.dao.TscEntraWorkDao;
import com.thinkgem.jeesite.modules.kaoqin.entrawork.entity.TscEntraWork;
import com.thinkgem.jeesite.modules.sys.dao.UserDao;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 加班表Service
 * @author xl
 * @version 2018-10-03
 */
@Service
@Transactional(readOnly = true)
public class TscEntraWorkService extends CrudService<TscEntraWorkDao, TscEntraWork> {
	
	private static final String GOOFF = " 17:00:00"; //下班時間
	private static final String ENTRATIME = " 19:00:00"; //開始計加班時間
	private static final long HOUR = 1000 * 60 * 60;
	private static final long HARF = 1000 * 60 * 30;
	
	@Autowired
	private UserDao userDao;
	
	public TscEntraWork get(String id) {
		return super.get(id);
	}

	@Transactional(readOnly = false)
	public void save(TscEntraWork tscEntraWork) {
		User user = tscEntraWork.getUser();
		if (user != null) {
			User u = userDao.get(user.getId());
			if (u != null) {
				tscEntraWork.setUserno(u.getNo());
			}
		}
		tscEntraWork.setEntraTime(sumEntraTime(tscEntraWork));
		super.save(tscEntraWork);
	}
	
	
	/**
	 * 加班時間
	 * @param tscEntraWork
	 * @return
	 */
	private double sumEntraTime(TscEntraWork tscEntraWork){
		Date begin = tscEntraWork.getBegintime();//前臺預設時間 17:00:00
		Date end = tscEntraWork.getEndtime();
		int entraTime = 0;
		try {
			if (DateUtils.isWeek(begin)) { 
				entraTime = (int) ((end.getTime() - begin.getTime())/HARF);
			}else{
				Date entra = DateUtils.convertTime(end, ENTRATIME);
				if (end.getTime() < entra.getTime()) {
					return 0;
				}
				entraTime = (int)((end.getTime() - begin.getTime())/HARF);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		double entra =entraTime/2 ; 
		return entra;
	}
	public static void main(String[] args) {

	}
}

(2)

	private double sumEntra(TscEntraWork tscEntraWork){
		Date begin = tscEntraWork.getBegintime();//前臺預設時間 17:00:00
		Date end = tscEntraWork.getEndtime();
		double entraTime = 0;
		try {
			if (DateUtils.isWeek(begin)) { 
				entraTime = ((end.getTime() - begin.getTime())/HOUR);
			}else{
				Date entra = DateUtils.convertTime(end, ENTRATIME);
				if (end.getTime() < entra.getTime()) {
					return 0;
				}
				entraTime = ((end.getTime() - begin.getTime())/HOUR);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		double floatEntra = entraTime - (int)(entraTime);
		if (0<=floatEntra &&floatEntra<0.5) {
			floatEntra = 0.0;
		}else if(0.5<=floatEntra && floatEntra<0.999999999){
			floatEntra = 0.5;
		}
		entraTime = (int)(entraTime) + floatEntra;
		return entraTime;
	}