java 8 日期函数
1、Timestamp(long)转成日期
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = timestamp.toLocalDateTime(); //之后就可以通过java8 新API操作时间了
localDateTime.toLocalDate();
Date date = new Date(timestamp.getTime()); //以前的Timetamp转日期的写法,然后通过SimpleDateFormatter转化了
2、Date与LocalDateTime的互相转换
/**
* LocalDateTime转Date
* @param localDateTime
* @return
*/
public static Date localDateTime2Date(LocalDateTime localDateTime){
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
Date date = Date.from(zonedDateTime.toInstant());
return date;
}
/**
* Date转LocalDateTime
* @param date
* @return
*/
public static LocalDateTime Date2LocalDateTime(Date date){
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
return localDateTime;
}
posted on 2018-11-15 22:28 Eee_xiang 阅读(1129) 评论(0) 编辑 收藏 举报