一、时间处理
(6条消息) (JAVA)hutool工具类-Date<——>String类型互转,加日期操作加一年、一月、一星期、一天、一分、一秒操作_hutool日期加一天_意梓的博客-CSDN博客
(10条消息) Hutool使用指南(一):时间日期工具_hutool日期比较_「已注销」的博客-CSDN博客
Java时间处理工具类TimeUtils_梦未的博客-CSDN博客
1、往前/后获取一天
java.util.的一个类
Calendar calendar = Calendar.getInstance();
calendar.setTime(end);
calendar.add(Calendar.DAY_OF_MONTH, 1);
Date startDate = Date.from(DateUtil.toInstant(start).plus(-1, TemporalUtil.toChronoUnit(TimeUnit.DAYS)));
DateUtil.offset(DateUtil.endOfDay(end), DateField.DAY_OF_MONTH, 1);
设置月初
public String getMonthStart(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(calendar.getTime());
}
设置月末
public String getMonthEnd(Date time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(calendar.getTime());
}
2、设置某天(long)的某刻时间
java.time.的两个类
// 当天的时间
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime dateTime = LocalDateTime.ofInstant(new Date(todayBeginTimeStamp * 1000).toInstant(), zoneId);
/ 某个时间点
dateTime = dateTime.withHour(8).withMinute(0).withSecond(0);
long morningWorkStartTime = Date.from(dateTime.atZone(zoneId).toInstant()).getTime() / 1000;
3、在Spring框架,将一个日期格式从后端传递给前端,可以通过注解@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")进行返回。
@JsonFormat(pattern = "yyyy-MM-dd HH")
private Date readyTime;
二、集合处理
1、cn.hutool.core.collection.CollUtil判断非空
CollUtil.isNotEmpty
2、cn.hutool.core.collection.CollectionUtil同上
3、java.util的Collections.sort:对集合的排序
自定义排序规则示例:总数-已收货数量=未收货数量。将未收货为0的放最后,其他的位置不动。
Collections.sort(list, (o1, o2) -> {
int differ1 = o1.getTotalNum() - o1.getReceivedNum();
int differ2 = o2.getTotalNum() - o2.getReceivedNum();
if (differ1 == 0) {
return 1;
}
if (differ2 == 0) {
return -1;
}
return 0;
});
参数说明:其中o1,o2表示集合在自然顺序中的待比较对象,return大于0表示按正序排列,小于0表示按逆序排列。等于0表示位置不动。
4、单个对象组装list
Collections.singletonList(Object)
持续更新记录中