先统计在22点后没有写工作记录的人员然后将没写的这些人的数据添加到工作表中 用java
假设工作表的数据结构为:
| 姓名 | 工作内容 | 时间 | | ---- | -------- | ---- | | 张三 | 编写文档 | 2021-08-01 08:30:00 | | 李四 | 编写代码 | 2021-08-01 22:30:00 | | 王五 | 进行测试 | 2021-08-01 16:45:00 |
我们需要先找出22点后没有写工作记录的人员,可以使用以下代码实现:
import java.time.*;
import java.time.format.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
// 假设当前时间为2021-08-01 23:00:00
LocalDateTime now = LocalDateTime.of(2021, 8, 1, 23, 0, 0);
List<String> noRecordNames = new ArrayList<>();
// 遍历工作表中的数据,判断是否在22点后有工作记录
List<Map<String, Object>> workRecords = getWorkRecords();
for (Map<String, Object> record : workRecords) {
String name = (String) record.get("姓名");
LocalDateTime time = LocalDateTime.parse((String) record.get("时间"), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
if (time.isAfter(LocalDateTime.of(now.toLocalDate(), LocalTime.of(22, 0, 0)))) {
noRecordNames.add(name);
}
}
// 将没写工作记录的人员的数据添加到工作表中
List<Map<String, Object>> newWorkRecords = new ArrayList<>(workRecords);
for (String name : noRecordNames) {
Map<String, Object> newRecord = new HashMap<>();
newRecord.put("姓名", name);
newRecord.put("工作内容", "");
newRecord.put("时间", now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
newWorkRecords.add(newRecord);
}
// 打印更新后的工作表
System.out.println("更新后的工作表:");
for (Map<String, Object> record : newWorkRecords) {
System.out.println(record.get("姓名") + "\t" + record.get("工作内容") + "\t" + record.get("时间"));
}
}
// 获取工作记录
private static List<Map<String, Object>> getWorkRecords() {
List<Map<String, Object>> workRecords = new ArrayList<>();
Map<String, Object> record1 = new HashMap<>();
record1.put("姓名", "张三");
record1.put("工作内容", "编写文档");
record1.put("时间", "2021-08-01 08:30:00");
workRecords.add(record1);
Map<String, Object> record2 = new HashMap<>();
record2.put("姓名", "李四");
record2.put("工作内容", "编写代码");
record2.put("时间", "2021-08-01 22:30:00");
workRecords.add(record2);
Map<String, Object> record3 = new HashMap<>();
record3.put("姓名", "王五");
record3.put("工作内容", "进行测试");
record3.put("时间", "2021-08-01 16:45:00");
workRecords.add(record3);
return workRecords;
}
}
输出结果为:
更新后的工作表:
张三 编写文档 2021-08-01 08:30:00
李四 编写代码 2021-08-01 22:30:00
王五 进行测试 2021-08-01 16:45:00
李四 2021-08-01 23:00:00
``
原文地址: https://www.cveoy.top/t/topic/cjRl 著作权归作者所有。请勿转载和采集!