ZoneId zone = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zone).toLocalDate(); 2.使用SimpleDateFormat: Date date =newDate(); SimpleDateFormat formatter =newSimpleDateFormat("yyyy-MM-dd"); String formattedDate = formatter.format(date); LocalDate localDate = LocalDate.parse(...
获得ZoneId的时间: //获得重庆的时间ZonedDateTime ChongqingTime =ZonedDateTime.now(Chongqing);System.out.println(ChongqingTime); 获得世界标准时间---本次子午线时间: System.out.println(ChongqingTime);//2024-01-07T09:54:36.623Z//获得世界标准时间--本初子午线ZonedDateTime utc =ZonedDateTime.now(Cloc...
LocalDate, LocalTime, LocalDateTime, ZoneId之间的关系可以用一张图解释:
LocalDate localDate=LocalDate.parse("2019-05-08");Instant instant=localDate.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();Date date=Date.from(instant);System.out.println(date);//Wed May 08 00:00:00 IST 2019 2. 使用 LocalDate.atStartOfDay 例1:LocalDate.atStartOf...
LocalTime time = LocalTime.of(15, 30, 20); int hour = time.getHour(); int minute = time.getMinute(); int second = time.getSecond(); 1. 2. 3. 4. 1 2 3 4 LocalDate和LocalTime都可以使用parse方法将字符串解析为LocalDate或LocalTime ...
LocalDate localDate = dateNew2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();System.out.println("当前date转日期对象:" + localDate);8. Date转LocalDateTime LocalDateTime localDateTime = dateNew2.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();System.out.println("当前date...
LocalDateTime localTime = LocalDateTime.now(); ZonedDateTime zonedTime = localTime.atZone(ZoneId.of("Asia/Shanghai")); 1. 2. 4. 结语 Java 8的日期和时间API简化了日期和时间的处理,但同时也需要我们注意一些细节,如日期格式、闰年问题和时区处理。通过合理使用LocalDate和LocalDateTime,可以编写出更稳定...
Long seconds=24*60*60L;//一天的秒数LocalDate localDate=LocalDate.ofInstant(Instant.ofEpochSecond(seconds),ZoneId.systemDefault());String format=localDate.format(DateTimeFormatter.ISO_DATE);System.out.println(format);//输出结果:1970-01-02} ...
LocalDate:本地日期,不包含具体时间 例如:2014-01-14 可以用来记录生日、纪念日、加盟日等。 LocalTime:本地时间,不包含日期。 LocalDateTime:组合了日期和时间,但不包含时差和时区信息。 ZonedDateTime:最完整的日期时间,包含时区和相对UTC或格林威治的时差。 新API还引入了 ZoneOffSet 和 ZoneId 类,使得解决时区...
LocalDate 与 Date之间的转换 //(1)LocalDate转化为Date ZonedDateTime zonedDateTime = LocalDate.now().atStartOfDay(ZoneId.systemDefault()); Date date = Date.from(zonedDateTime.toInstant()); System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date)); //(2)Date转化为LocalDate Lo...