关注我,升职加薪就是你!
好长时间没搞博客了,今天我们来搞搞Date、LocalDate、LocalTime、LocalDateTime、String以及时间戳的相互转换。话不多说,直接上代码。
package com.lovemax.test.common.utils;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* @Author: Max
* @Date: 2022/12/6
* @Description: 日期工具类
* @Version: v1.0
*/
public class DateUtil {
/**
* @Author: Max
* @Date: 2022/11/23
* @Description: 往前推几分钟
* @Param:
* @param date
* @param minutes 若 minutes=5,则为往前推5分钟
* @Return: java.util.Date
*/
public static Date minusMinutes(Date date, long minutes) {
LocalTime localTime = dateToLocalTime(date).minusMinutes(minutes);
LocalDate localDate = dateToLocalDate(date);
return localDateAndTimeToDate(localDate, localTime);
}
/**
* @Author: Max
* @Date: 2022/11/23
* @Description: LocalTime转Date
* @Param:
* @param localTime
* @Return: java.util.Date
*/
public static Date localDateAndTimeToDate(LocalDate localDate, LocalTime localTime) {
return Date.from(LocalDateTime.of(localDate, localTime).atZone(ZoneId.systemDefault()).toInstant());
}
/**
* @Author: Max
* @Date: 2022/11/23
* @Description: Date转LocalTime
* @Param:
* @param date
* @Return: java.time.LocalTime
*/
public static LocalTime dateToLocalTime(Date date) {
LocalDateTime localDateTime = dateToLocalDateTime(date);
return localDateTime.toLocalTime();
}
/**
* @Author: Max
* @Date: 2022/11/23
* @Description: Date 转 LocalDateTime
* @Param:
* @param date
* @Return: java.time.LocalDateTime
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
/**
* @Author: Max
* @Date: 2022/11/28
* @Description: Date 转 String
* @Param:
* @param date 日期
* @param pattern 返回日期的字符串格式 如“yyyy-MM-dd”
* @Return: java.lang.String
*/
public static String dateToString(Date date, String pattern) {
LocalDate localDate = dateToLocalDate(date);
return localDate.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* @Author: Max
* @Date: 2022/11/28
* @Description: Date 转 LocalDate
* @Param:
* @param date
* @Return: java.time.LocalDate
*/
public static LocalDate dateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* @Author: Max
* @Date: 2022/11/28
* @Description: String 转 LocalDate
* @Param:
* @param string
* @param pattern 返回日期的字符串格式 如“yyyy-MM-dd”
* @Return: java.lang.String
*/
public static LocalDate stringToLocalDate(String string, String pattern) {
return LocalDate.parse(string, DateTimeFormatter.ofPattern(pattern));
}
/**
* @Author: Max
* @Date: 2022/11/28
* @Description: String 转 Date
* @Param:
* @param string
* @param pattern
* @Return: java.util.Date
*/
public static Date stringToDate(String string, String pattern) {
LocalDate localDate = stringToLocalDate(string, pattern);
return localDateToDate(localDate);
}
/**
* @Author: Max
* @Date: 2022/11/28
* @Description: localDate转Date
* @Param:
* @param localDate
* @Return: java.util.Date
*/
public static Date localDateToDate(LocalDate localDate) {
Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* @Author: Max
* @Date: 2022/11/29
* @Description: 时间戳转LocalDate
* @Param:
* @param time
* @Return: java.time.LocalDate
*/
public static LocalDate longToLocalDate(long time) {
return Instant.ofEpochMilli(time).atZone(ZoneOffset.ofHours(8)).toLocalDate();
}
/**
* @Author: Max
* @Date: 2022/11/29
* @Description: 时间戳转LocalDateTime
* @Param:
* @param time
* @Return: java.time.LocalDateTime
*/
public static LocalDateTime longToLocalDateTime(long time) {
return Instant.ofEpochMilli(time).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
}
/**
* @Author: Max
* @Date: 2022/11/29
* @Description: LocalDateTime转Date
* @Param:
* @param localDateTime
* @Return: java.util.Date
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.toInstant(ZoneOffset.ofHours(8)));
}
/**
* @Author: Max
* @Date: 2022/12/5
* @Description: 往前推几小时
* @Param:
* @param date
* @param hours 若 hours=5,则为往前推1小时
* @Return: java.util.Date
*/
public static Date minusHours(Date date, long hours) {
LocalTime localTime = dateToLocalTime(date).minusHours(hours);
LocalDate localDate = dateToLocalDate(date);
return localDateAndTimeToDate(localDate, localTime);
}
public static void main(String[] args) {
long time = 1669630852714l;
LocalDate localDate = longToLocalDate(time);
System.out.println("localDate=" + localDate);
LocalDateTime localDateTime = longToLocalDateTime(time);
System.out.println("localDateTime=" + localDateTime);
Date date2 = localDateTimeToDate(localDateTime);
Date date = new Date(time);
Date minusFiveMinutesDate = minusMinutes(date,5);
System.out.println("minusFiveMinutesDate=" + minusFiveMinutesDate);
Date minusOneHoursDate = minusHours(date,1);
System.out.println(minusOneHoursDate);
}
}
好了,完事,趁着疫情别人都窝在家里玩游戏,赶紧卷起来吧!
关注我,升职加薪就是你!