赞
踩
java代码,根据程序员每天打卡时间,获取上班总时长,上班天数,平均上班时长,超出要求时间,为了方便大家把握时间节奏,合理规划下班时间;
package com.company.src.main.time; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; /** * @description 上班时长计算器 * @author liudi * @date 2022/1/17 18:00 * @version 1.0 **/ public class OfficeTime { public static void main(String[] args) { //要求上班时长(两次打卡时间间隔) int requireNum=11; List<BigDecimal> list= new ArrayList<>(); //第一周 list.add(getHours(new Time("09:45","20:45"))); list.add(getHours(new Time("09:44","20:34"))); list.add(getHours(new Time("09:48","22:34"))); list.add(getHours(new Time("09:46","20:03"))); //第二周 list.add(getHours(new Time("09:46","20:17"))); list.add(getHours(new Time("09:43","23:59"))); list.add(getHours(new Time("09:46","20:38"))); list.add(getHours(new Time("09:43","20:00"))); list.add(getHours(new Time("09:49","19:13"))); BigDecimal sum=BigDecimal.ZERO; for (BigDecimal bigDecimal : list) { sum=sum.add(bigDecimal); } System.out.println("----------------------汇总数据----------------------"); System.out.println("上班时长最大值:"+Collections.max(list)); System.out.println("上班时长最小值:"+Collections.min(list)); System.out.println("总上班时长:"+sum); System.out.println("上班天数:"+list.size()); System.out.println("平均每日上班时长:"+sum.divide(new BigDecimal(list.size()),2, BigDecimal.ROUND_HALF_UP)); System.out.println("超过界限小时数:"+sum.subtract(new BigDecimal(list.size()).multiply(new BigDecimal(requireNum)))); } /** * 获取某一天的上班时长,保留两位小数 * @param time * @return */ public static BigDecimal getHours(Time time){ String beginTime=time.getBeginTime(); Date beginDate = getDate(beginTime); String endTime=time.getEndTime(); Date endDate = getDate(endTime); long difference = endDate.getTime() - beginDate.getTime(); long diffHours = difference / (60 * 60 * 1000) % 24; long diffMinutes = (difference-diffHours*(60 * 60 * 1000)) / (60 * 1000) % 60; BigDecimal hours= new BigDecimal(diffHours); BigDecimal minutes= new BigDecimal(diffMinutes); BigDecimal divide = minutes.divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_HALF_UP); BigDecimal result=hours.add(divide); System.out.println("上班小时数为:"+diffHours+";上班时间分钟数是"+diffMinutes+";当天的上班时长为:"+result); return result; } /** * 根据时间字符串获取时间 * @param beginTime * @return */ public static Date getDate(String beginTime){ String startTime=beginTime+":00"; SimpleDateFormat dateFormat =new SimpleDateFormat("HH:mm:ss"); Date parse = null; try { parse = dateFormat.parse(startTime); } catch (ParseException e) { e.printStackTrace(); } return parse; } }
package com.company.src.main.time; /** * @description 时间类 * @author liudi * @date 2022/1/17 18:00 * @version 1.0 **/ public class Time { /** * 开始上班时间 */ private String beginTime; /** * 结束上班时间 */ private String endTime; public String getBeginTime() { return beginTime; } public void setBeginTime(String beginTime) { this.beginTime = beginTime; } public String getEndTime() { return endTime; } public void setEndTime(String endTime) { this.endTime = endTime; } public Time(String beginTime, String endTime) { this.beginTime = beginTime; this.endTime = endTime; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。