Java8中LocalDateTime与时间戳timestamp的互相转换Java8 LocalDateTime与timestamp转换
将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 = Instant(ZoneOffset.of("+8")).toEpochMilli();
return timestamp;
}
我在⽹上还到了另⼀个将datetime转为时间戳的⽅法:
ZoneId zone = ZoneId.systemDefault();
long timestamp = ldt.atZone(zone).toInstant().toEpochMilli();
Java8的时间转为时间戳的⼤概的思路就是LocalDateTime先转为Instant,设置时区,然后转timestamp。
附⼀个Java8中的LocalDateTime⼯具类
⼯具类
package com.kingboymon.utils.date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import poral.ChronoUnit;
import poral.TemporalUnit;
import java.util.Date;
/*
* @author kingboy
* @Date 2017/7/22 下午2:12
* @Description LocalDateTimeUtils is used to Java8中的时间类
*/
public class LocalDateTimeUtils {
//获取当前时间的LocalDateTime对象
//w();
//根据年⽉⽇构建LocalDateTimejava时间日期格式转换
//LocalDateTime.of();
/
/⽐较⽇期先后
//w().isBefore(),
//w().isAfter(),
//Date转换为LocalDateTime
public static LocalDateTime convertDateToLDT(Date date) {
return LocalDateTime.Instant(), 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 w(), 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) Years();
if (field == ChronoUnit.MONTHS) Years() * 12 + Months();
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.kingboymon.localdatetimeutils;
import com.kingboymon.utils.date.LocalDateTimeUtils;
import org.junit.Test;
import java.time.LocalDateTime;
import poral.ChronoUnit;
import static com.kingboymon.utils.DayEnd;
import static com.kingboymon.utils.DayStart;
/**
* @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));
//=============================================================================================
/*
年:1
⽉:13
⽇:396
半⽇:792
⼩时:9506
分钟:570362
秒:34221720
毫秒:34221720000
*/
}
@Test
public void plus_test() {
//增加⼆⼗分钟
System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.w(),
20,
ChronoUnit.MINUTES), "yyyy年MM⽉dd⽇ HH:mm"));
/
/增加两年
System.out.println(LocalDateTimeUtils.formatTime(LocalDateTimeUtils.w(),
2,
ChronoUnit.YEARS), "yyyy年MM⽉dd⽇ HH:mm"));
//=============================================================================================
/*
2017年07⽉22⽇ 22:53
2019年07⽉22⽇ 22:33
*/
}
@Test
public void dayStart_test() {
System.out.println(w()));
System.out.println(w()));
//=============================================================================================
/*
2017-07-22T00:00
2017-07-22T23:59:59.999999999
*/
}
}
总结
到此这篇关于Java8中LocalDateTime与时间戳timestamp互相转换的⽂章就介绍到这了,更多相关Java8 LocalDateTime与timestamp转换内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。