通过把字段设置为String, java和MongoDb之间可以无缝进行时间操作:
获取24小时之前的时间
Instant.ofEpochMilli(Instant.now().toEpochMilli() - 1000 * 3600 * 24)
根据字符串获取LocalDateTime,例如:"2018-11-25T13:07:53.243Z"
LocalDateTime date = LocalDateTime.ofInstant(Instant.parse(dateString), ZoneId.of("UTC"));
生成休息日日历
for(int dateIncrease = 0; dateIncrease <= 365; dateIncrease ++) {
WeekEndDate wd = new WeekEndDate();
long days = 3600 * 24 * dateIncrease;
Instant currentDate = Instant.ofEpochSecond(Instant.parse("2021-01-01T00:00:00.000Z").toEpochMilli() / 1000 + days);
LocalDateTime date = LocalDateTime.ofInstant(currentDate, ZoneId.of(ZoneId.SHORT_IDS.get("CTT")));
wd.setDate(currentDate + "");
if(date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY) {
wd.setWeekend(true);
}
Cheers,
Good Luck!