开发考勤系统时涉及的日期时间计算

=================比较2个时间并获取其差额换算成小时返回

public float getDateHour(Date endDate, Date nowDate) {
//    public long getDateHour(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
//        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        float minPerHour = Float.valueOf(min) / 60;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;

        float rstTmp = Float.valueOf(hour) + minPerHour;

// 修改为这种方式,不会导致4舍5入的情况
        String tmp = String.valueOf(rstTmp);
        int idx = tmp.indexOf(".");
        tmp = tmp.substring(0, idx + 1);

//        float rst = (float) (Math.round(rstTmp * 10) / 10);

// 之前为这种方式,会导致4舍5入的情况
//        DecimalFormat df = new DecimalFormat(".0");
//        df.setRoundingMode(RoundingMode.HALF_UP);
//        String rstStr = df.format(rstTmp);
//        System.out.println("申请开始结束时间差额小时数-----》》》》》》》" + rstStr);
//        return Float.valueOf(rstStr);
        return Float.valueOf(tmp);

//        return hour;
    }

=================日期格式化:

// 日期转换为字符串
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfHour = new SimpleDateFormat("HH:mm:ss");
String sdStrTmp = sdf.format(json.getStartDate());
String sdStrYearTmp = sdStrTmp.substring(0, 10);
String sdStrHourTmp = sdStrTmp.substring(11, 19);
String edStrTmp = sdf.format(json.getEndDate());
String edStrYearTmp = edStrTmp.substring(0, 10);
String edStrHourTmp = edStrTmp.substring(11, 19);
// 字符串转换为日期
try {
    // 不管是当天,还是连续2天,开始时间必须小于结束时间
    Date sdTmp = sdf.parse(sdStrTmp);
    Date edTmp = sdf.parse(edStrTmp);
} catch (ParseException e) {
    throw new BizException("加班申请时间数据异常,请重试或联系管理员!");
}

=================根据到岗日期ArrivalDate获取员工司龄

 Double companyYear = -1D;
            try {
                Date arrivalDate = sdf.parse(sdf.format(employee.getArrivalDate()));
                Date now = sdf.parse(sdf.format(new Date()));
                Calendar cal = Calendar.getInstance();
                cal.setTime(arrivalDate);
                long arrivalTime = cal.getTimeInMillis();
                cal.setTime(now);
                long nowTime = cal.getTimeInMillis();
                long betweenDays = (nowTime - arrivalTime) / (1000 * 3600 * 24);
                companyYear = Double.valueOf(betweenDays) / 365;
            } catch (ParseException e) {
                throw new BizException("获取司龄数据异常,请重试或联系管理员!");
            }
            if (companyYear < 0) {
                companyYear = 0D;
//                throw new BizException("获取司龄数据异常,请重试或联系管理员!");
            }
            employee.setCompanyYear(companyYear);

=================日期加一天
Calendar sdCalYear = Calendar.getInstance();
sdCalYear.setTime(sdfYear.parse(sdStrYearTmp));
sdCalYear.add(Calendar.DATE, 1);// 加一天之后看是否和结束时间一样

=================分钟换算成毫秒
int afterNum = null == hrKqruleOvertimeType.getAfterNum() ? 0 : hrKqruleOvertimeType.getAfterNum().intValue();// 加班规则:下班多少分钟后,开始算加班时间
int afterNumHaoMiao = afterNum * 60 * 1000;// 换算成毫秒
Date workdate1Start = shift.getWorkdate1Start();
long workdate1StartReal = shift.getWorkdate1Start().getTime();
workdate1EndReal = workdate1End.getTime() + afterNumHaoMiao;

=================日期比较

// 日期比较,不建议用这一种compareTo,因为当2个日期格式化时如果不一致时会导致比较不正确的情况:

//            return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
if (sdTmp.compareTo(edTmp) != -1) {// 开始时间不能大于结束时间校验
    throw new BizException("申请开始时间必须在申请结束时间之前,请重试或联系管理员!");
}
// 日期比较,建议用这一种getTime:
if (json.getStartDate().getTime() >= json.getEndDate().getTime()) {
    throw new BizException("开始时间必须小于结束时间,请重试或联系管理员!");
}
=================根据指定的年和小时获取一个新的日期
private Date getNewDate(Date year, Date hour) {
        Calendar cyear = Calendar.getInstance();
        cyear.setTime(year);

        Calendar chour = Calendar.getInstance();
        chour.setTime(hour);

        cyear.set(Calendar.HOUR_OF_DAY, chour.get(Calendar.HOUR_OF_DAY));
        cyear.set(Calendar.MINUTE, chour.get(Calendar.MINUTE));
        cyear.set(Calendar.SECOND, chour.get(Calendar.SECOND));

        Date d = cyear.getTime();
        return d;

//        System.out.println("年: " + now.get(Calendar.YEAR));
//
//        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
//
//        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
//
//        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
//
//        System.out.println("分: " + now.get(Calendar.MINUTE));
//
//        System.out.println("秒: " + now.get(Calendar.SECOND));
//
//        System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
    }

=================取整

//                向上取整:Math.ceil(double a)
//                向下取整:Math.floor(double a)
//                四舍五入取整:Math.round(double a)
//                例:
//                Math.ceil(24.1)--> 25
//                Math.floor(24.8)--> 24
//                Math.round(24.1)--> 24
//                Math.round(24.8)--> 25
                int tmp = (int) Math.floor(sumTimeNumOvertime.getSumTimeNum());
                leave.setHourNum(Integer.valueOf(tmp));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值