二 时间戳与LocalDateTime互转 2.1 LocalDateTime 转 时间戳
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @Test public void localTimeTest1 () { LocalDateTime localDateTime = LocalDateTime.now(); long second = localDateTime.toEpochSecond(ZoneOffset.ofHours(8 )); System.out.println(second); } @Test public void localTimeTest2 () { LocalDateTime localDateTime = LocalDateTime.now(); long second = localDateTime.toInstant(ZoneOffset.ofHours(8 )).getEpochSecond(); System.out.println(second); } public void localTimeTest3 () { LocalDateTime localDateTime = LocalDateTime.now(); long milliseconds =localDateTime.toInstant(ZoneOffset.ofHours(8 )).toEpochMilli(); System.out.println(milliseconds/1000 ); }
2.2 时间戳 转LocalDateTime 以下几种获取的LocalDateTime方式按读者需求进行获取,不同的精确值,将获取不同的结果;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public void localTimeTest4 () { long second = LocalDateTime.now().toInstant(ZoneOffset.of("+8" )).getEpochSecond(); LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0 , ZoneOffset.ofHours(8 )); System.out.println(localDateTime); } public void localTimeTest5 () { long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8" )).toEpochMilli(); LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(milliseconds/1000 , 0 , ZoneOffset.ofHours(8 )); System.out.println(localDateTime); } @Test public void localTimeTest6 () { long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8" )).toEpochMilli(); LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8 )).toLocalDateTime(); System.out.println(localDateTime); }
三 时间戳与LocalDate互转 学会时间戳与LocalDate互转,同理就可以推出时间戳与LocalTime 互转,不过知识追寻者相信几乎没人会用到这个,故这边就不做示例;
3.1 时间戳转LocalDate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @Test public void localDateTest1 () { long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8" )).toEpochMilli(); LocalDate localDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8 )).toLocalDate(); System.out.println(localDate); } @Test public void localDateTest2 () { long seconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8" )).getEpochSecond(); LocalDate localDate = Instant.ofEpochSecond(seconds).atZone(ZoneOffset.ofHours(8 )).toLocalDate(); System.out.println(localDate); }
3.2 LocalDate 转 时间戳
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @Test public void localDateTest3 () { LocalDate localDate = LocalDate.now(); long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8 )).toInstant().getEpochSecond(); System.out.println(seconds); } @Test public void localDateTest4 () { LocalDate localDate = LocalDate.now(); long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8 )).toInstant().toEpochMilli(); System.out.println(seconds); }
四 LocalDateTime与Date互转 4.1 Date转LocalDateTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Test public void DateTest1 () { Date date = new Date(); LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.ofHours(8 )).toLocalDateTime(); System.out.println(localDateTime); } @Test public void DateTest2 () { Date date = new Date(); long second = date.toInstant().atOffset(ZoneOffset.ofHours(8 )).toEpochSecond(); LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0 , ZoneOffset.ofHours(8 )); System.out.println(localDateTime); }
4.2 LocalDateTime 转 Date
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Test public void DateTest3 () { LocalDateTime localDateTime = LocalDateTime.now(); Instant instant = Instant.ofEpochSecond(localDateTime.toEpochSecond(ZoneOffset.ofHours(8 ))); Date date = Date.from(instant); System.out.println(date); } @Test public void DateTest4 () { LocalDateTime localDateTime = LocalDateTime.now(); Instant instant = localDateTime.atZone(ZoneOffset.ofHours(8 )).toInstant(); Date date = Date.from(instant); System.out.println(date); }
五 LocalDate与Date互转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public void DateTest5 () { LocalDate localDate = LocalDate.now(); Instant instant = localDate.atStartOfDay(ZoneOffset.ofHours(8 )).toInstant(); Date date = Date.from(instant); System.out.println(date); } public void DateTest6 () { Date date = new Date(); LocalDate localDate = date.toInstant().atOffset(ZoneOffset.ofHours(8 )).toLocalDate(); System.out.println(localDate); }
六 LocalDateTime格式化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public void format1 () { LocalDateTime localDateTime = LocalDateTime.now(); DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss" ); String format = localDateTime.format(dateTimeFormatter); System.out.println(format); } public void format2 () { String time = "2020/02/03 14/38/54" ; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss" ); LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter); System.out.println(localDateTime); }