LocalDate/LocalDateTime/LocalTime/Instant...

1.LocalDate/LocalDateTime/LocalTime/Instant...:

存储在java.time.*包中:值不可变!。

调整时差必须重新赋值。

1.Instant:

 

 2.LocalDateTime:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public String addAttendance(HttpSession session) { Integer userId = (Integer) session.getAttribute("userid"); // Integer userId =1; LocalDateTime now = LocalDateTime.now(); Attendance lastAttendance = attendanceService.getByAscTime(userId); Attendance attendance = new Attendance(); attendance.setUserid(userId); attendance.setDate(Date.from(now.atZone(ZoneId.systemDefault()).toInstant())); // 获取当前本机时间 LocalDate localDate = now.toLocalDate(); LocalTime localTime = now.toLocalTime(); // 判断数据库没有打卡记录或者有下班签到 if (lastAttendance == null || lastAttendance.getType().equals("下班签到")) { if (localTime.isBefore(LocalTime.of(9, 0))) { attendance.setType("上班签到"); } else if (localDate.equals(getLastAttendanceDate(lastAttendance))) { attendance.setType("下班签到"); } else if (localTime.isAfter(LocalTime.of(9, 0))){ attendance.setType("迟到"); }else if (localTime.isBefore(LocalTime.of(9, 17))){ attendance.setType("早退"); } } else { if (localDate.equals(getLastAttendanceDate(lastAttendance))) { if (localTime.isBefore(LocalTime.of(9, 0))) { attendance.setType("上班签到"); } else if (localTime.isBefore(LocalTime.of(17, 0))){ attendance.setType("早退"); }else if (localTime.isBefore(LocalTime.of(17, 0))){ attendance.setType("下班签到"); } } else { if (localTime.isBefore(LocalTime.of(9, 0))) { attendance.setType("上班签到"); } else if (localTime.isAfter(LocalTime.of(9, 0))){ attendance.setType("迟到"); }else if (localTime.isBefore(LocalTime.of(17, 0))){ attendance.setType("早退"); } } } attendanceService.save(attendance); return "success"; } private LocalDate getLastAttendanceDate(Attendance lastAttendance) { if (lastAttendance == null) { return null; } Date date = lastAttendance.getDate(); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); return instant.atZone(zoneId).toLocalDate(); } }
06-01
这段代码是一个Java方法,用于添加考勤记录。接收一个`HttpSession`对象作为参数,从中获取当前用户的id,然后根据当前时间判断考勤类型,并将考勤记录保存到数据库中。 下面是具体的解释: - `Integer userId = (Integer) session.getAttribute("userid")`:从`HttpSession`对象中获取当前用户的id。 - `LocalDateTime now = LocalDateTime.now()`:获取当前时间。 - `Attendance lastAttendance = attendanceService.getByAscTime(userId)`:根据用户id获取最近一次考勤记录。 - `Attendance attendance = new Attendance()`:创建一个新的考勤记录。 - `attendance.setUserid(userId)`:将当前用户的id设置为考勤记录的用户id。 - `attendance.setDate(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()))`:将当前时间转换成`java.util.Date`类型,并设置为考勤记录的日期。 - `if (lastAttendance == null || lastAttendance.getType().equals("下班签到"))`:判断最近一次考勤记录是否为空或者为“下班签到”。如果是,则当前考勤记录为“上班签到”或“迟到”或“早退”。如果不是,则当前考勤记录为“下班签到”或“早退”。 - `if (localTime.isBefore(LocalTime.of(9, 0)))`:判断当前时间是否早于上午9点。 - `else if (localDate.equals(getLastAttendanceDate(lastAttendance)))`:判断当前日期是否与最近一次考勤记录的日期相同。 - `else if (localTime.isAfter(LocalTime.of(9, 0)))`:判断当前时间是否晚于上午9点。 - `else if (localTime.isBefore(LocalTime.of(9, 17)))`:判断当前时间是否早于上午9点17分。 - `else if (localTime.isBefore(LocalTime.of(17, 0)))`:判断当前时间是否早于下午5点。 - `else if (localTime.isBefore(LocalTime.of(17, 0)))`:判断当前时间是否早于下午5点。 - `attendanceService.save(attendance)`:将考勤记录保存到数据库。 - `return "success"`:返回一个字符串表示操作成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值