买完网站怎么建设工程接单网

当前位置: 首页 > news >正文

买完网站怎么建设,工程接单网,河南省的网页制作,来返苏州人员信息登记平台Date: 你不爱我了吗? #x1f6a1;本地时间时区相关格式化在Java 8中#xff0c;Instant类用于表示时间戳#xff0c;相当于旧的Date类#xff1b;LocalDateTime类用于表示日期和时间#xff0c;相当于旧的Calendar类#xff1b;DateTimeFormatter类用于格式化日期和时间… Date: 你不爱我了吗? 本地时间时区相关格式化在Java 8中Instant类用于表示时间戳相当于旧的Date类LocalDateTime类用于表示日期和时间相当于旧的Calendar类DateTimeFormatter类用于格式化日期和时间相当于旧的SimpleDateFormat类本地时间 LocalDate表示日期如2023-04-06。 LocalDate now LocalDate.now(); // 获取当前日期 LocalDate date LocalDate.of(2023, 4, 6); // 通过年、月、日创建LocalDate对象 LocalDate plusDate date.plusDays(1); // 增加1天 LocalDate minusDate date.minusDays(1); // 减少1天 int year date.getYear(); // 获取年份 int month date.getMonthValue(); // 获取月份 int day date.getDayOfMonth(); // 获取日期 LocalTime表示时间如14:30:00。 LocalTime now LocalTime.now(); // 获取当前时间 LocalTime time LocalTime.of(14, 30); // 通过小时和分钟创建LocalTime对象 LocalTime plusTime time.plusHours(1); // 增加1小时 LocalTime minusTime time.minusMinutes(30); // 减少30分钟 int hour time.getHour(); // 获取小时 int minute time.getMinute(); // 获取分钟 LocalDateTime表示日期时间如2023-04-06T14:30:00。 LocalDateTime now LocalDateTime.now(); // 获取当前日期时间 LocalDateTime dateTime LocalDateTime.of(2023, 4, 6, 14, 30); // 通过年、月、日、小时和分钟创建LocalDateTime对象 LocalDateTime plusDateTime dateTime.plusDays(1); // 增加1天 LocalDateTime minusDateTime dateTime.minusHours(2); // 减少2小时 int year dateTime.getYear(); // 获取年份 int month dateTime.getMonthValue(); // 获取月份 int day dateTime.getDayOfMonth(); // 获取日期 int hour dateTime.getHour(); // 获取小时 int minute dateTime.getMinute(); // 获取分钟 Instant表示时间戳即从1970年1月1日00:00:00开始的毫秒数。 Instant now Instant.now(); // 获取当前时间戳 Instant instant Instant.ofEpochMilli(1000); // 通过时间戳创建Instant对象 Instant plusInstant instant.plusSeconds(10); // 增加10秒 Instant minusInstant instant.minusMillis(500); // 减少500毫秒 long epochMilli instant.toEpochMilli(); // 获取从1970年1月1日00:00:00开始的毫秒数 Duration表示时间间隔如2小时30分钟。 LocalDateTime start LocalDateTime.of(2023, 4, 6, 14, 30); LocalDateTime end LocalDateTime.of(2023, 4, 6, 16, 0); Duration duration Duration.between(start, end); // 计算两个时间点之间的时间间隔 Duration plusDuration duration.plusHours(1); // 增加1小时 Duration minusDuration duration.minusMinutes(30); // 减少30分钟 long hours duration.toHours(); // 获取总小时数 long minutes duration.toMinutes(); // 获取总分钟数 Period表示日期间隔如2天。 LocalDate start LocalDate.of(2023, 4, 6); LocalDate end LocalDate.of(2023, 4, 8); Period period Period.between(start, end); // 计算两个日期之间的日期间隔 Period plusPeriod period.plusDays(1); // 增加1天 Period minusPeriod period.minusWeeks(1); // 减少1周 int days period.getDays(); // 获取天数 int months period.getMonths(); // 获取月数 这些类都是线程安全的不可变的因此适合在多线程环境下使用。 在Java中这些时间类的作用非常广泛它们可以帮助我们处理日期时间相关的逻辑例如计算时间差、格式化日期时间、比较时间、转换时区等等。在实际开发中我们常常需要进行时间计算例如计算两个时间点之间的时间差、计算某个日期是周几等等。这些时间类可以大大简化这些操作使得我们的代码更加简洁、清晰、易于维护。 时区相关 ZoneId时区标识符表示不同的时区例如“Asia/Shanghai”。 ZoneId systemZone ZoneId.systemDefault(); // 获取系统默认时区 ZoneId beijingZone ZoneId.of(Asia/Shanghai); // 根据时区ID创建ZoneId对象 ZonedDateTime带时区的日期时间可以表示不同时区下的日期时间例如“2023-04-06T14:30:0008:00[Asia/Shanghai]”。 ZonedDateTime now ZonedDateTime.now(); // 获取当前日期时间和时区信息的ZonedDateTime对象 ZonedDateTime zonedDateTime ZonedDateTime.of(LocalDateTime.of(2023, 4, 6, 14, 30), ZoneId.of(Asia/Shanghai)); // 根据本地日期时间和时区创建ZonedDateTime对象 ZonedDateTime withZone zonedDateTime.withZoneSameInstant(ZoneId.of(America/New_York)); // 修改时区并且保持日期时间不变 int year zonedDateTime.getYear(); // 获取年份 int month zonedDateTime.getMonthValue(); // 获取月份 int day zonedDateTime.getDayOfMonth(); // 获取日期 int hour zonedDateTime.getHour(); // 获取小时 int minute zonedDateTime.getMinute(); // 获取分钟 ZoneId zone zonedDateTime.getZone(); // 获取时区信息 OffsetDateTime带时区偏移量的日期时间可以表示相对于UTC的偏移量例如“2023-04-06T14:30:0008:00”。 OffsetDateTime now OffsetDateTime.now(); // 获取当前日期时间和时区偏移量信息的OffsetDateTime对象 OffsetDateTime offsetDateTime OffsetDateTime.of(LocalDateTime.of(2023, 4, 6, 14, 30), ZoneOffset.of(08:00)); // 根据本地日期时间和时区偏移量创建OffsetDateTime对象 OffsetDateTime plusOffsetDateTime offsetDateTime.plusHours(1); // 增加1小时 OffsetDateTime minusOffsetDateTime offsetDateTime.minusMinutes(30); // 减少30分钟 int year offsetDateTime.getYear(); // 获取年份 int month offsetDateTime.getMonthValue(); // 获取月份 int day offsetDateTime.getDayOfMonth(); // 获取日期 int hour offsetDateTime.getHour(); // 获取小时 int minute offsetDateTime.getMinute(); // 获取分钟 ZoneOffset offset offsetDateTime.getOffset(); // 获取时区偏移量信息 ZoneOffset时区偏移量表示相对于UTC的偏移量例如“08:00”。 ZoneOffset offset1 Zone这些与时区相关的时间类可以帮助我们处理跨时区的时间计算和表示。在处理全球化应用时时区相关的操作尤其重要因为不同地区的时间可能有巨大的差异。这些时间类提供了强大的功能来处理跨时区的时间计算和表示使得我们可以更好地应对全球化应用的时间需求。 格式化 在JDK8中如果要对时间进行格式化可以使用以下几个类 DateTimeFormatter日期时间格式化类它提供了很多方法来将日期时间格式化为字符串或解析字符串为日期时间。 SimpleDateFormat日期时间格式化类它是JDK早期版本中的日期时间格式化类在JDK8中仍然保留。它使用一些预定义的格式化字符串来格式化日期时间。
这两个类都提供了格式化日期时间的功能但是使用方式略有不同。DateTimeFormatter提供了更加灵活的API来定制日期时间格式它支持解析和格式化ISO 8601格式的日期时间字符串并且可以支持多种语言环境。SimpleDateFormat则更适合于一些简单的日期时间格式化场景它使用的是预定义的格式化字符串虽然使用起来比较简单但是可定制性较差。 下面是一个使用DateTimeFormatter对日期时间进行格式化的示例代码 LocalDateTime dateTime LocalDateTime.of(2023, 4, 6, 14, 30); DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss); String formattedDateTime dateTime.format(formatter); // 将日期时间格式化为指定格式的字符串 LocalDateTime parsedDateTime LocalDateTime.parse(2023-04-06 14:30:00, formatter); // 将指定格式的字符串解析为日期时间 上面的代码使用DateTimeFormatter将当前时间格式化为“yyyy-MM-dd HH:mm:ss”的字符串格式。