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
| System.out.println("下面的是两个默认时区的LocalDateTime"); final LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()); final LocalDateTime localDateTime1 = LocalDateTime.now(); System.out.println(localDateTime); System.out.println(localDateTime1); System.out.println("下面的是两个通过Date.from()拿到的默认时区的Date");
final Date from = Date.from(Instant.from(ZonedDateTime.now()));
final Date from1 = Date.from(localDateTime1.toInstant(OffsetDateTime.now().getOffset())); System.out.println(from); System.out.println(from1); System.out.println("下面的是通过LocalDateTime.ofInstant()拿到的不同时区的LocalDateTime");
final LocalDateTime localDateTime2 = LocalDateTime.ofInstant(Instant.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)), ZoneId.systemDefault());
final LocalDateTime localDateTime3 = LocalDateTime.ofInstant(Instant.from(LocalDateTime.now().toInstant(ZoneOffset.of("+08:00"))), ZoneId.systemDefault()); System.out.println(localDateTime2); System.out.println(localDateTime3); System.out.println("下面的是通过Date.from()拿到的不同时区的date");
final Date from2 = Date.from(Instant.from(LocalDateTime.now().toInstant(ZoneOffset.UTC)));
final Date from3 = Date.from(Instant.from(LocalDateTime.now().toInstant(ZoneOffset.MIN)));
final Date from4 = Date.from(Instant.from(LocalDateTime.now().toInstant(ZoneOffset.MAX)));
final Date from5 = Date.from(Instant.from(LocalDateTime.now().toInstant(ZoneOffset.of("+8")))); System.out.println(from2); System.out.println(from3); System.out.println(from4); System.out.println(from5);
|