java
@Test
public void validateDateUtils(){
// 1 字符串转换日期
Date result = DateUtil.parse("2023-08-01", com.alibaba.excel.util.DateUtils.DATE_FORMAT_10);
log.info("result : [{}]" , result);
// 2 日期转换字符串
final Date date = new Date();
String formatStr = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_19);
log.info("formatStr : [{}]" , formatStr);
// 3 获取当前时间,格式为yyyy-MM-dd,yyyy-MM-dd HH:mm:ss,yyyyMMddHHmmss
final String formatYMR = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_10);
log.info("formatYMR : [{}]" , formatYMR);
final String formatYMRSFM = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_19);
log.info("formatYMRSFM : [{}]" , formatYMRSFM);
final String formatYMRSFMS = DateFormatUtils.format(date, com.alibaba.excel.util.DateUtils.DATE_FORMAT_14);
log.info("formatYMRSFMS : [{}]" , formatYMRSFMS);
// 4 日期相减
final long resultTime = result.getTime();
final long dateTIme = date.getTime();
final long diffInDays = (dateTIme - resultTime) / (1000 * 60 * 60 * 24);
log.info("diffInDays : [{}]" , diffInDays);
// 5 日期加减操作
final int compareDays = cn.hutool.core.date.DateUtil.compare(date, result);
log.info("compareDays : [{}]" , compareDays);
final Date addDate = DateUtils.addDays(new Date(), 1);
log.info("addDate : [{}]" , addDate);
// 6 日期转换时间戳
final long current = cn.hutool.core.date.DateUtil.current();
final long currentTimeMillis = System.currentTimeMillis();
log.info("currentTimeMillis : [{}]" , currentTimeMillis);
log.info("current : [{}]" , current);
// 7 util.date转换sql.date
Timestamp timestamp = new Timestamp(currentTimeMillis);
log.info("timestamp : [{}]" , timestamp);
}
22:49:41.918 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - result : [Tue Aug 01 00:00:00 CST 2023]
22:49:42.102 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatStr : [2023-08-09 22:49:41]
22:49:42.103 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatYMR : [2023-08-09]
22:49:42.103 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatYMRSFM : [2023-08-09 22:49:41]
22:49:42.106 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - formatYMRSFMS : [20230809224941]
22:49:42.106 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - diffInDays : [8]
22:49:42.121 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - compareDays : [1]
22:49:42.121 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - addDate : [Thu Aug 10 22:49:42 CST 2023]
22:49:42.122 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - currentTimeMillis : [1691592582122]
22:49:42.122 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - current : [1691592582122]
22:49:42.123 [main] INFO com.geekmice.springbootselfexercise.NoDaoTest - timestamp : [2023-08-09 22:49:42.122]