1.日期处理
1.1 引入必要依赖:版本5.3.8
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<artifactId>hutool-core</artifactId>
<groupId>cn.hutool</groupId>
<version>${hutool.version}</version>
<dependency>
2.1 指定日期格式转换
DateTime date = DateUtil.date(Calendar.getInstance());
String format = "yyyy-MM-dd HH:mm";
String d = DateUtil.format(date, format);
System.out.println("d = " + d);
2.2 指定增加n天后的日期
String time = "2020-02-13";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(time);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date); //date放到Calender对象中
calendar.add(Calendar.DATE, 7);//后增加n天
Date result = calendar.getTime();
String format = sdf.format(result);
System.out.println("date = " + format);
} catch (Exception e) {
e.printStackTrace();
}
2.3 判断二个日期之间相差几天 相差几年,相差几月
LocalDateTime currentTime = LocalDateTime.now();
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String aa = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
LocalDateTime collectTimeDate2 = LocalDateTime.parse(aa,df2);
//判断相差多少天 相差几年,相差几月,相差几天 (四舍五入后的效果)
long between = ChronoUnit.MONTHS.between(collectTimeDate2, currentTime);
System.out.println(between);
标签:总结,相差,String,format,处理,hutool,yyyy,日期,date
From: https://www.cnblogs.com/qxlzzj/p/17516883.html