参考文档
转换时间戳为Java 8时间
/** * 转换时间戳为 LocalDate * * @param epochMilli 时间戳 */ public LocalDate getLocalDate(long epochMilli) { return Instant.ofEpochMilli(epochMilli).atZone(ZoneOffset.of("+8")).toLocalDate(); } /** * 转换时间戳为 LocalDateTime * * @param epochMilli 时间戳 */ public LocalDateTime getLocalDateTime(long epochMilli) { return Instant.ofEpochMilli(epochMilli).atZone(ZoneOffset.of("+8")).toLocalDateTime(); }
Java 8时间转Date
/** * LocalDate 转为Date */ public Date localDateToDate(LocalDate localDate) { Instant instant = localDate.atStartOfDay().atZone(ZoneId.of("Asia/Shanghai")).toInstant(); return Date.from(instant); }
常用工具类
import com.anchi.car.coresystem.consumer.dao.entity.CitySupportDO; import com.anchi.car.coresystem.consumer.dao.mapper.CitySupportMapper; import com.anchi.car.coresystem.consumer.dao.query.CitySupportQuery; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.Locale; /** * @author: shaoshuaui.zheng * @description: * @date: 2019/1/2 18:28 */ @Component public class DateUtil { private static final String LANG_ZH_CN = "zh-cn"; /** * 根据语言类型格式化一个字符串日期 * * @param date 格式化日期 * @param lang 语言类型 zh-cn 中文 en 英文 * @return 格式化的日期 */ public String dateFormat(Date date, String lang) { String dateTimeFormatter; if (StringUtils.equalsIgnoreCase(LANG_ZH_CN, lang)) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); dateTimeFormatter = dateFormat.format(date); } else { SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d yyyy HH:mm:ss", Locale.ENGLISH); dateTimeFormatter = dateFormat.format(date); } return dateTimeFormatter; } /** * 根据语言类型格式化一个字符串日期 * * @param localDateTime 格式化日期 * @param lang 语言类型 zh-cn 中文 en 英文 * @return 格式化的日期 */ public String dateFormat(LocalDateTime localDateTime, String lang) { String dateTimeFormatter; if (StringUtils.isBlank(lang)) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); dateTimeFormatter = localDateTime.format(formatter); return dateTimeFormatter; } if (StringUtils.equalsIgnoreCase(LANG_ZH_CN, lang)) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); dateTimeFormatter = localDateTime.format(formatter); } else { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm", Locale.ENGLISH); dateTimeFormatter = localDateTime.format(formatter); } return dateTimeFormatter; } /** * 返回格式化好的时间:2019-01-17 * * @param date * @return */ public String simpleDateFormat1(Date date) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); return simpleDateFormat.format(date); } /** * 返回格式化好的时间:2019-01-1 02:12 * * @param date * @return */ public static String middleDateFormat(Date date) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return simpleDateFormat.format(date); } public String middleLocalDateTimeFormat(LocalDateTime localDateTime) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); return localDateTime.format(formatter); } /** * 转换当地时间为北京时区 * * @param localDateTime 当地时间 * @param toZoneId 当地时区id * @return 北京时间 */ public LocalDateTime conversionToBeijingDateTime(LocalDateTime localDateTime, String toZoneId) { // 转换当前时间为当地时区 ZonedDateTime dateAndTimeInLocal = ZonedDateTime.of(localDateTime, ZoneId.of(toZoneId)); // 转换为北京时间: ZonedDateTime dateTimeInBeijing = dateAndTimeInLocal.withZoneSameInstant(ZoneId.of("Asia/Shanghai")); return dateTimeInBeijing.toLocalDateTime(); } /** * 转换当地时间为北京时区 * * @param localDateTime 当地时间戳 * @param toZoneId 当地时区id * @return 北京时间 */ public LocalDateTime conversionToBeijingDateTime(Long localDateTime, String toZoneId) { // 时间戳转为当地时间 Instant instant = Instant.ofEpochMilli(localDateTime); LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant, ZoneId.of(toZoneId)); // 转换当前时间为当地时区 return conversionToBeijingDateTime(localDateTime1, toZoneId); } /** * 相差多少分钟 * * @param fromDate * @param toDate * @return */ public static int getIntervalMinutes(Date fromDate, Date toDate) { return (int) ((toDate.getTime() - fromDate.getTime()) / (60 * 1000)); } }