介绍
在以往的JAVA开发中,我们只能使用Date作为日期开发的常用工具,但是随着时代的发展,对日期的操作有了更高的需求,Date无法满足,并且使用起来代码比较繁琐,在此情景下,JAVA8引入了新的日期类-LocalDate以及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
| public static void main(String[] args) {
Instant instant = Instant.now(); System.out.println(instant);
LocalDate localDate = LocalDate.now(); System.out.println(localDate);
LocalTime localTime = LocalTime.now(); System.out.println(localTime);
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime);
ZonedDateTime zonedDateTime = ZonedDateTime.now(); System.out.println(zonedDateTime); }
|
LocalDate与Date及String的相互转化
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 40 41
| public static void main(String[] args) {
String str = "2019-11-17"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate localDate = LocalDate.parse(str, formatter); System.out.println(localDate);
Date date = new Date(); Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); localDate = instant.atZone(zoneId).toLocalDate(); System.out.println(localDate);
localDate = LocalDate.now(); ZonedDateTime zdt = localDate.atStartOfDay(zoneId); date = Date.from(zdt.toInstant()); System.out.println(date);
instant = Instant.ofEpochMilli(System.currentTimeMillis()); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); System.out.println(localDateTime);
LocalDateTime now = LocalDateTime.now(); long beijing = now.toInstant(ZoneOffset.ofHours(8)).toEpochMilli(); System.out.println(beijing); long nowTime = now.toInstant(ZoneOffset.of("+08:00")).toEpochMilli(); System.out.println(nowTime); long normalTime = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println(normalTime); }
|
LocalDate和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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| public static void main(String[] args) {
LocalDate date = LocalDate.of(2019, 11, 17);
LocalDateTime time = LocalDateTime.of(date, LocalTime.of(12, 0, 0));
LocalDate dateFrom = LocalDate.of(2018, 11, 11); LocalDate now = LocalDate.now(); long between = ChronoUnit.DAYS.between(dateFrom, now);
boolean leapYear = LocalDate.now().isLeapYear();
LocalDate plusDays = now.plusDays(100);
LocalDate minusDays = now.minusDays(100);
LocalDate plusMonths = now.plusMonths(3);
LocalDate minusMonths = now.minusMonths(3);
LocalDate plusWeeks = now.plusWeeks(1);
LocalDate minusWeeks = now.minusWeeks(1);
LocalDate plusYears = now.plusYears(1);
LocalDate minusYears = now.minusYears(1);
LocalDate withDayOfMonth = now.withDayOfMonth(19);
LocalDate withDayOfYear = now.withDayOfYear(10);
LocalDate withMonth = now.withMonth(10);
LocalDate withYear = now.withYear(2018); }
|
结束语
更多更好用的,等你来发掘……