多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## LocalDate、LocalDateTime与timestamp、Date的转换 #### 1.LocalDate转Date ``` LocalDate nowLocalDate = LocalDate.now(); Date date = Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant()); ``` #### 2.LocalDateTime转Date ``` LocalDateTime localDateTime = LocalDateTime.now(); Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant()); ``` #### 3.Date转LocalDateTime(LocalDate) ``` Date date = new Date(); LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime(); LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate(); ``` #### 4.LocalDate转时间戳 ``` LocalDate localDate = LocalDate.now(); long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli(); ``` #### 5.LocalDateTime转时间戳 ``` LocalDateTime localDateTime = LocalDateTime.now(); long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli(); ``` #### 6.时间戳转LocalDateTime(LocalDate) ``` long timestamp = System.currentTimeMillis(); LocalDate localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate(); LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime(); ``` #### 7.工具类 ``` import com.alibaba.druid.util.StringUtils; import org.springframework.util.Assert; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalUnit; import java.util.Calendar; import java.util.Date; /** * jdk1.8使用 * 日期时间工具类 */ public class DateTimeUtils { public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss"); public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static final String DATE_FORMATTER_CON = "yyyyMMdd"; public static final long tenSecond= 30 * 1000; /** * Date转换为LocalDateTime * * @param date * @return */ public static LocalDateTime convertToLocalDateTime(Date date) { return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); } /** * local时间转换成UTC时间 * @param currentTimeStamp * @return */ public static Date localToUTC(Long currentTimeStamp) { String localTime = DateTimeUtils.convertTimeToString(currentTimeStamp); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date localDate= null; try { localDate = sdf.parse(localTime); } catch (ParseException e) { e.printStackTrace(); } long localTimeInMillis=localDate.getTime(); /** long时间转换成Calendar */ Calendar calendar= Calendar.getInstance(); calendar.setTimeInMillis(localTimeInMillis); /** 取得时间偏移量 */ int zoneOffset = calendar.get(Calendar.ZONE_OFFSET); /** 取得夏令时差 */ int dstOffset = calendar.get(Calendar.DST_OFFSET); /** 从本地时间里扣除这些差量,即可以取得UTC时间*/ calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset)); /** 取得的时间就是UTC标准时间 */ Date utcDate=new Date(calendar.getTimeInMillis()); return utcDate; } /** * LocalDateTime转换为Date * * @param dateTime * @return */ public static Date convertToDate(LocalDateTime dateTime) { return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant()); } /** * 返回本地当前日期 * * @return */ public static LocalDate getCurrentLocalDate() { return LocalDate.now(); } /** * 返回本地当前时间 * * @return */ public static LocalTime getCurrentLocalTime() { return LocalTime.now(); } /** * 返回本地当前日期时间 * * @return */ public static LocalDateTime getCurrentLocalDateTime() { return LocalDateTime.now(); } /** * 返回本地当前日期的固定或自定义字符串 * * @return */ public static String getCurrentDateStr(String pattern) { if (StringUtils.isEmpty(pattern)) { return LocalDate.now().format(DateTimeFormatter.ofPattern(pattern)); } return LocalDate.now().format(DATE_FORMATTER); } /** * 返回本地当前时间的固定或自定义字符串 * * @return */ public static String getCurrentTimeStr(String pattern) { if (StringUtils.isEmpty(pattern)) { return LocalTime.now().format(DateTimeFormatter.ofPattern(pattern)); } return LocalTime.now().format(TIME_FORMATTER); } /** * 返回本地当前日期时间的固定或自定义字符串 * * @return */ public static String getCurrentDateTimeStr(String pattern) { if (StringUtils.isEmpty(pattern)) { return LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern)); } return LocalDateTime.now().format(DATETIME_FORMATTER); } /** * 将本地日期字符串根据pattern解析出本地日期 * * @param dateStr * @param pattern * @return */ public static LocalDate parseLocalDate(String dateStr, String pattern) { return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern)); } /** * 将本地时间字符串根据pattern解析出本地时间 * * @param timeStr * @param pattern * @return */ public static LocalTime parseLocalTime(String timeStr, String pattern) { return LocalTime.parse(timeStr, DateTimeFormatter.ofPattern(pattern)); } /** * 将本地日期字符串根据pattern解析出本地日期 * * @param dateTimeStr * @param pattern * @return */ public static LocalDateTime parseLocalDateTime(String dateTimeStr, String pattern) { return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(pattern)); } /** * 将本地日期转化为固定或自定义格式的字符串 * * @param date * @return */ public static String formatLocalDate(LocalDate date, String pattern) { if (StringUtils.isEmpty(pattern)) { return date.format(DateTimeFormatter.ofPattern(pattern)); } return date.format(DATE_FORMATTER); } /** * 将本地时间转化为固定或自定义格式的字符串 * * @param time * @return */ public static String formatLocalTime(LocalTime time, String pattern) { if (StringUtils.isEmpty(pattern)) { return time.format(DateTimeFormatter.ofPattern(pattern)); } return time.format(TIME_FORMATTER); } /** * 将本地日期时间转化为固定或自定义格式的字符串 * * @param datetime * @return */ public static String formatLocalDateTime(LocalDateTime datetime, String pattern) { if (StringUtils.isEmpty(pattern)) { return datetime.format(DateTimeFormatter.ofPattern(pattern)); } return datetime.format(DATETIME_FORMATTER); } /** * 获取指定日期的秒 * * @param dateTime * @return */ public static Long getSeconds(LocalDateTime dateTime) { return dateTime.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond(); } /** * 获取指定日期的毫秒(转时间戳) * @param dateTime * @return */ public static Long getMillis(LocalDateTime dateTime) { return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /** * 字符串转转时间戳 * @param time * @return */ public static Long strToLongGetMillis(String time) { return LocalDateTime.parse(time, DATETIME_FORMATTER).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } /** * 时间戳转LocalDateTime * * @param timestamp * @return */ public static LocalDateTime timestampToLDT(Long timestamp) { return LocalDateTime.ofEpochSecond(timestamp / 1000, (int) (timestamp % 1000) * 1000000, ZoneOffset.ofHours(8)); } /** * 本地日期时间加上一个数 * * @param dateTime * @param num * @param field ChronoUnit.*(年、月、周、日、分...) * @return */ public static LocalDateTime plus(LocalDateTime dateTime, long num, TemporalUnit field) { return dateTime.plus(num, field); } /** * 本地日期时间减去一个数 * * @param dateTime * @param num * @param field ChronoUnit.*(年、月、周、日、分...) * @return */ public static LocalDateTime minu(LocalDateTime dateTime, long num, TemporalUnit field) { return dateTime.minus(num, field); } /** *本地日期时间减去一个数 Date时间格式 * @param dateTime * @param num * @param field ChronoUnit.*(年、月、周、日、分...) * @return */ public static Date getMinu(Date dateTime, long num, TemporalUnit field) { LocalDateTime localDateTime = DateTimeUtils.minu(DateTimeUtils.convertToLocalDateTime(dateTime), num, field); return DateTimeUtils.convertToDate(localDateTime); } /** *本地日期时间加一个数 Date时间格式 * @param dateTime * @param num * @param field ChronoUnit.*(年、月、周、日、分...) * @return */ public static Date getPlus(Date dateTime, long num, TemporalUnit field) { LocalDateTime localDateTime = DateTimeUtils.plus(DateTimeUtils.convertToLocalDateTime(dateTime), num, field); return DateTimeUtils.convertToDate(localDateTime); } /** * 获取本地两个日期的时间差 * * @param startTime * @param endTime * @param field * @return */ public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, TemporalUnit field) { Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime)); if (field == ChronoUnit.YEARS) { return period.getYears(); } else if (field == ChronoUnit.MONTHS) { return period.getYears() * 12 + period.getMonths(); } return field.between(startTime, endTime); } /** * 根据LocalDateTime获取一天的开始时间 * * @param dateTime * @return */ public static LocalDateTime getDateStart(LocalDateTime dateTime) { return dateTime.withHour(0).withMinute(0).withSecond(0).withNano(0); } /** * 根据LocalDateTime获取一天的结束时间 * * @param dateTime * @return */ public static LocalDateTime getDateEnd(LocalDateTime dateTime) { return dateTime.withHour(23).withMinute(59).withSecond(59).withNano(999999999); } /** * 根据LocalDate获取一天的结束时间 * * @param date * @return */ public static LocalDateTime getLocalDateEnd(LocalDate date) { return date.atTime(23, 59, 59, 999999999); } /** * 根据LocalDate获取一天的开始时间 * * @param date * @return */ public static LocalDateTime getLocalDateStart(LocalDate date) { return date.atTime(0, 0, 0, 0); } /** * 是否当天 * * @param dateTime * @return */ public static boolean isToday(LocalDateTime dateTime) { return getCurrentLocalDate().equals(dateTime.toLocalDate()); } //获取x个月前的时间点 public static String getTwoTimePoint(int x){ LocalDateTime now = LocalDateTime.now(); now = now.minus(x, ChronoUnit.MONTHS); return now.format(DATETIME_FORMATTER); } /** * 时间戳转字符串 * 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss * @param time * @return */ public static String convertTimeToString(Long time){ Assert.notNull(time, "time is null"); return DATETIME_FORMATTER.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault())); } } ```