工具类测试结果
23:55:42.982 [main] INFO com.atguigu.util.DateUtil - ---------------------------------------------------------------------------
23:55:43.020 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21
23:55:43.021 [main] INFO com.atguigu.util.DateUtil - 当前时间为:23:55:43.021
23:55:43.021 [main] INFO com.atguigu.util.DateUtil - 当前日期时间为:2021-04-21T23:55:43.021
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期时间格式化为:2021-04-21 23:55:43
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - ---------------------------------------------------------------------------
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,前一秒为:2021-04-21T23:55:42.029
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,前一分为:2021-04-21T23:54:43.029
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,前一时为:2021-04-21T22:55:43.029
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,前一天为:2021-04-20T23:55:43.029
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,前一周为:2021-04-14T23:55:43.029
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,前一月为:2021-03-21T23:55:43.029
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,前一年为:2020-04-21T23:55:43.029
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - ---------------------------------------------------------------------------
23:55:43.029 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,后一秒为:2021-04-21T23:55:44.029
23:55:43.030 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.029,后一分为:2021-04-21T23:56:43.029
23:55:43.030 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.030,后一时为:2021-04-22T00:55:43.030
23:55:43.030 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.030,后一天为:2021-04-22T23:55:43.030
23:55:43.030 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.030,后一周为:2021-04-28T23:55:43.030
23:55:43.030 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.030,后一月为:2021-05-21T23:55:43.030
23:55:43.030 [main] INFO com.atguigu.util.DateUtil - 当前日期为:2021-04-21T23:55:43.030,后一年为:2022-04-21T23:55:43.030
23:55:43.030 [main] INFO com.atguigu.util.DateUtil - ---------------------------------------------------------------------------
23:55:43.033 [main] INFO com.atguigu.util.DateUtil - 2021-04-19 23:00:00 与 2021-04-21 00:00:00 相差 2 天
23:55:43.033 [main] INFO com.atguigu.util.DateUtil - ---------------------------------------------------------------------------
23:55:43.033 [main] INFO com.atguigu.util.DateUtil - 将:2021-04-21T23:55:43.033 转换为毫秒:1619020543033
23:55:43.033 [main] INFO com.atguigu.util.DateUtil - 将:2021-04-21T23:55:43.033 转换为秒 :1619020543
23:55:43.033 [main] INFO com.atguigu.util.DateUtil - 将:2021-04-21T23:55:43.033 转换为时间戳-1 :1619020543033
23:55:43.033 [main] INFO com.atguigu.util.DateUtil - 将:2021-04-21T23:55:43.033 转换为时间戳-2 :1619020543033
23:55:43.033 [main] INFO com.atguigu.util.DateUtil - 将:2021-04-21T23:55:43.033 转换为时间戳-3 :1619020543033
工具类:DateUtil
package com.atguigu.util;
import lombok.extern.slf4j.Slf4j;
import java.time.*;
import java.time.format.DateTimeFormatter;
/**
* Description: 日期工具类
*
* @Author: zhx & moon hongxu_1234@163.com
* @Date: 2021-04-21 22:49
* @version: V1.0.0
*/
@Slf4j
public class DateUtil {
private static String DETAIL_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
private static String PROBABLY_FORMAT_STRING = "yyyy-MM-dd";
private static String TIME_FORMAT_STRING = "HH:mm:ss";
/**
* 打印当前日期-时间
*/
public static void printCurrentTime(){
log.info("当前日期为:{}",LocalDate.now());
log.info("当前时间为:{}", LocalTime.now());
log.info("当前日期时间为:{}", LocalDateTime.now());
log.info("当前日期时间格式化为:{}", formatToStr(LocalDateTime.now(),DETAIL_FORMAT_STRING));
}
/**
* 计算当前时间的前多久
*/
public static void lastTime(){
log.info("当前日期为:{},前一秒为:{}",LocalDateTime.now(),LocalDateTime.now().minusSeconds(1));
log.info("当前日期为:{},前一分为:{}",LocalDateTime.now(),LocalDateTime.now().minusMinutes(1));
log.info("当前日期为:{},前一时为:{}",LocalDateTime.now(),LocalDateTime.now().minusHours(1));
log.info("当前日期为:{},前一天为:{}",LocalDateTime.now(),LocalDateTime.now().minusDays(1));
log.info("当前日期为:{},前一周为:{}",LocalDateTime.now(),LocalDateTime.now().minusWeeks(1));
log.info("当前日期为:{},前一月为:{}",LocalDateTime.now(),LocalDateTime.now().minusMonths(1));
log.info("当前日期为:{},前一年为:{}",LocalDateTime.now(),LocalDateTime.now().minusYears(1));
}
/**
* 计算当前时间的后多久
*/
public static void nextTime(){
log.info("当前日期为:{},后一秒为:{}",LocalDateTime.now(),LocalDateTime.now().plusSeconds(1));
log.info("当前日期为:{},后一分为:{}",LocalDateTime.now(),LocalDateTime.now().plusMinutes(1));
log.info("当前日期为:{},后一时为:{}",LocalDateTime.now(),LocalDateTime.now().plusHours(1));
log.info("当前日期为:{},后一天为:{}",LocalDateTime.now(),LocalDateTime.now().plusDays(1));
log.info("当前日期为:{},后一周为:{}",LocalDateTime.now(),LocalDateTime.now().plusWeeks(1));
log.info("当前日期为:{},后一月为:{}",LocalDateTime.now(),LocalDateTime.now().plusMonths(1));
log.info("当前日期为:{},后一年为:{}",LocalDateTime.now(),LocalDateTime.now().plusYears(1));
}
/**
* 获取指定日期的毫秒
* @param date
* @return
*/
public static void getMilliByTime(LocalDateTime date) {
log.info("将:{} 转换为毫秒:{}",date,date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
}
/**
* 获取指定日期的秒
* @param date
* @return
*/
public static void getSecondsByTime(LocalDateTime date) {
log.info("将:{} 转换为秒 :{}",date,date.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond());
}
/**
* LocalDateTime转换时间戳
* @param date
*/
public static void localDateTimeToTimesTamp(LocalDateTime date) {
log.info("将:{} 转换为时间戳-1 :{}",date,date.toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
log.info("将:{} 转换为时间戳-2 :{}",date,date.toInstant(ZoneOffset.of("+08:00")).toEpochMilli());
log.info("将:{} 转换为时间戳-3 :{}",date,date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
}
/**
* 计算两个日期之间间隔的天数
* 此方法会将:LocalDateTime 转为 LocalDate 再计算
* @param start
* @param end
*/
public static void period(String start,String end,String format){
LocalDate startDate = format(start,format).toLocalDate();
LocalDate endDate = format(end,format).toLocalDate();
int daysNum=(int)(endDate.toEpochDay() - startDate.toEpochDay());
log.info("{} 与 {} 相差 {} 天",start,end,daysNum);
}
/**
* 将日期时间类型格式化为字符串
* @param date
* @param format
* @return
*/
public static String formatToStr(LocalDateTime date,String format){
return date.format(dateTimeFormatter(format));
}
/**
* 将日期时间格式的字符串格式化为LocalDateTime
* @param date
* @param format
* @return
*/
public static LocalDateTime format(String date,String format){
return LocalDateTime.parse(date, dateTimeFormatter(format));
}
/**
* 获取格式化格式
* @param format
* @return
*/
public static DateTimeFormatter dateTimeFormatter(String format){
return DateTimeFormatter.ofPattern(format);
}
public static void main(String[] args) {
log.info("---------------------------------------------------------------------------");
printCurrentTime();
log.info("---------------------------------------------------------------------------");
lastTime();
log.info("---------------------------------------------------------------------------");
nextTime();
log.info("---------------------------------------------------------------------------");
period("2021-04-19 23:00:00","2021-04-21 00:00:00",DETAIL_FORMAT_STRING);
log.info("---------------------------------------------------------------------------");
LocalDateTime date = LocalDateTime.now();
getMilliByTime(date);
getSecondsByTime(date);
localDateTimeToTimesTamp(date);
}
}