java8已经推出了五六年时间了,项目中也经常用到它的一些特性,但是在日期使用方面,没有进行过较为全面的梳理,临近年关,今日得空整理出来,一来备忘而来分享。
日期时间方面的常用类有三个LocalDate、LocalTime 、LocalDateTime ,最为常用当属LocalDateTime
以上三种使用方式大同小异,着重记录下LocalDateTime 的使用
import org.junit.Test;
import java.util.Date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class LocalDateDemo {
@Test
public void testLocalDate() {
LocalDate today = LocalDate.now();
System.out.println("今天的日期:"+ today);
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.println("year:"+year);
System.out.println("month:"+month);
System.out.println("day:"+day);
LocalDate previousYear = today.minus(1, ChronoUnit.YEARS);
System.out.println("一年前的日期 : " + previousYear);
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
System.out.println("一年后的日期:"+nextYear);
LocalDate previousWeek = today.minus(1, ChronoUnit.WEEKS);
System.out.println("一周前的日期为:"+previousWeek);
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
System.out.println("一周后的日期为:"+nextWeek);
LocalDate date = LocalDate.of(2018,2,6);
System.out.println("自定义日期:"+date);
System.out.println("日期比较:"+today.equals(date));
System.out.println("previousWeek早于nextWeek:"+previousWeek.isBefore(nextWeek));
}
@Test
public void testLocalTime() {
LocalTime time = LocalTime.now();
System.out.println("获取当前的时间,不含有日期:"+time);
LocalTime previousTime = time.minusHours(3);
System.out.println("三个小时前的时间为:"+previousTime);
LocalTime newTime = time.plusHours(3);
System.out.println("三个小时后的时间为:"+newTime);
//分秒同理~
}
@Test
public void testLocalDateTime() {
LocalDateTime now = LocalDateTime.now();
System.out.println("获取当前的时间,含有日期:"+now);
//自定义时间,可精确到纳秒
LocalDateTime time = LocalDateTime.of(2020,01,21,9,44, 37, 20111111);
System.out.println("自定义时间:"+time);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String formatStr = time.format(formatter);
System.out.println("时间转格式化字符串:"+ formatStr);
LocalDateTime parse = LocalDateTime.parse(formatStr, formatter);
System.out.println("格式化字符串转时间:"+ parse);
//加减年月日时分秒
LocalDateTime previousTime = time.minusHours(3);
System.out.println("三个小时前的时间为:"+previousTime);
LocalDateTime newTime = time.plusHours(3);
System.out.println("三个小时后的时间为:"+newTime);
System.out.println("修该年:"+LocalDate.now().withYear(2019));
System.out.println("判断当前是否闰年:"+LocalDate.now().isLeapYear());
//分秒同理~
//获取间隔时间
Duration duration = Duration.between(previousTime,newTime);
System.out.println("相距日:"+duration.toDays());
System.out.println("相距时:"+duration.toHours());
System.out.println("相距分:"+duration.toMinutes());
System.out.println("相距秒:"+duration.toMillis());
//LocalDateTime与Date之间互转
Date date = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
LocalDateTime dateTime = LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault());
System.out.println("LocalDateTime转Date:" + date);
System.out.println("Date转LocalDateTime:" + dateTime);
//时间戳
System.out.println("时间戳(秒):"+LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond());
System.out.println("时间戳(秒):"+Instant.now().getEpochSecond());
System.out.println("时间戳(毫秒):"+LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
System.out.println("时间戳(毫秒):"+Instant.now().toEpochMilli());
}
}