JDK1.8新特性-日期时间

具体代码详见:https://github.com/LynHB/ProjectA/blob/master/src/main/java/study/date/LocalDateTimeTest.java
相比于SimpleDateFormat,线程安全。

1. LocalDate日期类

LocalDate JDK新增日期类

System.out.println("LocalDate演示:");
LocalDate localDate = LocalDate.now();
System.out.println("LocalDate打印:"+localDate);
System.out.println("LocalDate获取年:"+localDate.getYear());
System.out.println("LocalDate获取月:"+localDate.getMonthValue());
System.out.println("LocalDate获取日:"+localDate.getDayOfMonth());
System.out.println("LocalDate获取周几:"+localDate.getDayOfWeek().getValue());
System.out.println("LocalDate获取周(英文):"+localDate.getDayOfWeek().name());

2.LocalTime时间类

System.out.println("LocalTime演示:");
LocalTime localTime = LocalTime.now();
System.out.println("LocalTime获取小时:"+localTime.getHour());
System.out.println("LocalTime获取分钟:"+localTime.getMinute());
System.out.println("LocalTime获取秒:"+localTime.getSecond());

3 LocalDateTime日期时间类

LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("LocalDateTime演示:");
System.out.println("LocalDateTime获取年:"+localDateTime.getYear());
System.out.println("LocalDateTime获取月:"+localDateTime.getMonthValue());
System.out.println("LocalDateTime获取日:"+localDateTime.getDayOfMonth());
System.out.println("LocalDateTime获取周几:"+localDateTime.getDayOfWeek().getValue());
System.out.println("LocalDateTime获取周(英文):"+localDateTime.getDayOfWeek().name());
System.out.println("LocalDateTime获取小时:"+localDateTime.getHour());
System.out.println("LocalDateTime获取分钟:"+localDateTime.getMinute());
System.out.println("LocalDateTime获取秒:"+localDateTime.getSecond());

4 Instant实例类

Instant instant = Instant.now();
System.out.println("Instant演示:");
System.out.println("Instant获取秒数:"+instant.getEpochSecond());
System.out.println("Instant获取毫秒数:"+instant.toEpochMilli());

5 LocalDateTime深入

LocalDate和LocalTime同理。

5.1 修改LocalDateTime具体时间

LocalDateTime localDateTime1 = LocalDateTime.now();
System.out.println("针对LocalDateTime进行修改转换:");
System.out.println("LocalDateTime减少一年:"+localDateTime1.minus(1, ChronoUnit.YEARS));
System.out.println("LocalDateTime增加一月:"+localDateTime1.plusMonths(1));
System.out.println("LocalDateTime修改成固定年2011:"+localDateTime1.withYear(2011));

5.2 字符串转换LocalDateTime

String dateStr = "2020-01-02 11:11:11";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
localDateTime = LocalDateTime.parse(dateStr,dtf);

5.3 LocalDateTime转字符串

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String  dateStr = localDateTime.format(dtf);

5.4 LocalDateTime转Date

localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());

5.5 localDateTime转时间戳

localDateTime = LocalDateTime.now();
long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();

5.6 时间戳转LocalDateTime

timestamp = System.currentTimeMillis();
localDate = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDate();
localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();

5.7 LocalDate转LocalDateTime

localDateTime = LocalDateTime.of(localDate,LocalTime.MIN);

5.8 LocalDate转时间戳

localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();

你可能感兴趣的:(JDK1.8新特性-日期时间)