时间:2021-05-20
将timestamp转为LocalDateTime
public LocalDateTime timestamToDatetime(long timestamp){ Instant instant = Instant.ofEpochMilli(timestamp); return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); }将LocalDataTime转为timestamp
public long datatimeToTimestamp(LocalDateTime ldt){ long timestamp = ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli(); return timestamp; }我在网上还找到了另一个将datetime转为时间戳的方法:
ZoneId zone = ZoneId.systemDefault();long timestamp = ldt.atZone(zone).toInstant().toEpochMilli();Java8的时间转为时间戳的大概的思路就是LocalDateTime先转为Instant,设置时区,然后转timestamp。
工具类
package com.kingboy.common.utils.date;import java.time.*;import java.time.format.DateTimeFormatter;import java.time.temporal.ChronoUnit;import java.time.temporal.TemporalUnit;import java.util.Date;/* * @author kingboy * @Date 2017/7/22 下午2:12 * @Description LocalDateTimeUtils is used to Java8中的时间类 */public class LocalDateTimeUtils { //获取当前时间的LocalDateTime对象 //LocalDateTime.now(); //根据年月日构建LocalDateTime //LocalDateTime.of(); //比较日期先后 //LocalDateTime.now().isBefore(), //LocalDateTime.now().isAfter(), //Date转换为LocalDateTime public static LocalDateTime convertDateToLDT(Date date) { return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); } //LocalDateTime转换为Date public static Date convertLDTToDate(LocalDateTime time) { return Date.from(time.atZone(ZoneId.systemDefault()).toInstant()); } //获取指定日期的毫秒 public static Long getMilliByTime(LocalDateTime time) { return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } //获取指定日期的秒 public static Long getSecondsByTime(LocalDateTime time) { return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond(); } //获取指定时间的指定格式 public static String formatTime(LocalDateTime time,String pattern) { return time.format(DateTimeFormatter.ofPattern(pattern)); } //获取当前时间的指定格式 public static String formatNow(String pattern) { return formatTime(LocalDateTime.now(), pattern); } //日期加上一个数,根据field不同加不同值,field为ChronoUnit.* public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) { return time.plus(number, field); } //日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.* public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field){ return time.minus(number,field); } /** * 获取两个日期的差 field参数为ChronoUnit.* * @param startTime * @param endTime * @param field 单位(年月日时分秒) * @return */ public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) { Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime)); if (field == ChronoUnit.YEARS) return period.getYears(); if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths(); return field.between(startTime, endTime); } //获取一天的开始时间,2017,7,22 00:00 public static LocalDateTime getDayStart(LocalDateTime time) { return time.withHour(0) .withMinute(0) .withSecond(0) .withNano(0); } //获取一天的结束时间,2017,7,22 23:59:59.999999999 public static LocalDateTime getDayEnd(LocalDateTime time) { return time.withHour(23) .withMinute(59) .withSecond(59) .withNano(999999999); }}测试类
package com.kingboy.common.localdatetimeutils;import com.kingboy.common.utils.date.LocalDateTimeUtils;import org.junit.Test;import java.time.LocalDateTime;import java.time.temporal.ChronoUnit;import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayEnd;import static com.kingboy.common.utils.date.LocalDateTimeUtils.getDayStart;/** * @author kingboy * @Date 2017/7/22 下午7:16 * @Description LocaDateTimeUtilsTest is used to 测试LocalDateTime工具 */public class LocaDateTimeUtilsTest { @Test public void format_test() { System.out.println(LocalDateTimeUtils.formatNow("yyyy年MM月dd日 HH:mm:ss")); } @Test public void betweenTwoTime_test() { LocalDateTime start = LocalDateTime.of(1993, 10, 13, 11, 11); LocalDateTime end = LocalDateTime.of(1994, 11, 13, 13, 13); System.out.println("年:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.YEARS)); System.out.println("月:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MONTHS)); System.out.println("日:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.DAYS)); System.out.println("半日:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.HALF_DAYS)); System.out.println("小时:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.HOURS)); System.out.println("分钟:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MINUTES)); System.out.println("秒:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.SECONDS)); System.out.println("毫秒:" + LocalDateTimeUtils.betweenTwoTime(start, end, ChronoUnit.MILLIS)); //============================================================================================= } @Test public void plus_test() { //增加二十分钟 System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(), 20, ChronoUnit.MINUTES), "yyyy年MM月dd日 HH:mm")); //增加两年 System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.plus(LocalDateTime.now(), 2, ChronoUnit.YEARS), "yyyy年MM月dd日 HH:mm")); //============================================================================================= } @Test public void dayStart_test() { System.out.println(getDayStart(LocalDateTime.now())); System.out.println(getDayEnd(LocalDateTime.now())); //============================================================================================= }}到此这篇关于Java8中LocalDateTime与时间戳timestamp互相转换的文章就介绍到这了,更多相关Java8 LocalDateTime与timestamp转换内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
LocalDate、LocalTime、LocalDateTime是Java8开始提供的时间日期API,主要用来优化Java8以前对于时间日期的处理操作。然而,
简述时间日期处理是平时工作中使用非常频繁的逻辑,Java8中提供的新的时间类LocalDateTime和LocalDate,使日期处理可以更简单。友情提醒下,业
这篇文章主要介绍了java8时间转换(LocalDateTime)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
下面总结一下js中时间戳与日期格式的相互转换: 1.将时间戳转换成日期格式: functiontimestampToTime(timestamp){va
前言Java8提供了一套新的时间api,比之前的Calendar类要简单明了很多。常用的有三个类Instant、LocalDate、LocalDateTime,