java8 时间操作

2 篇文章 0 订阅

Instant         时间戳
Duration        持续时间、时间差
LocalDate       只包含日期,比如:2018-09-24
LocalTime       只包含时间,比如:10:32:10
LocalDateTime   包含日期和时间,比如:2018-09-24 10:32:10
Peroid          时间段
ZoneOffset      时区偏移量,比如:+8:00
ZonedDateTime   带时区的日期时间
Clock           时钟,可用于获取当前时间戳
java.time.format.DateTimeFormatter      时间格式化类

 

获取当前时间  LocalDateTime time = LocalDateTime.now();

日期格式:DateTimeFormatter formatter= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

java8当前时间转String字符串:String tm = time.format(formatter);

将Date时间转为String:Date date = new Date();

   Instant instant =date.toInstant();

   ZoneId zoneId = ZoneId.systemDefault();

   LocalDateTime localDateTime =LocalDateTime.ofInstant(instant,zoneId);

   String tm = formatter.format(localDateTime);

将String转为Date类型:String tm8="2020-11-11 08:00:00";

     LocalDateTime localDateTime=LocalDateTime.parse(tm8,formatter);

     ZoneId zoneId = ZoneId.systemDefault();

     Instant instant = localDateTime.atZone(zoneId).toInstant();

     Date btm = Date.from(instant);

LocalDate 转Date类型:LocalDate localDate =LocalDate.now();
     ZonedDateTime zonedDateTime = dateTime.atStartOfDay(ZoneId.systemDefault());

      Instant instant = zonedDateTime.toInstant();

      Date btm = Date.from(instant);

      如果是string类型的LocalDate只需要将String转为LocalDate,如

      DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

      LocalDate dateTime = LocalDate.parse(stringtm, dateTimeFormatter);

      LocalDate.parse(stringtm,dateTimeFormatter );

Date转LocalDate:

       Date date  = new Date();

       Instant instant = date.toInstant();

       ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());

       LocalDate localDate = zdt.toLocalDate();

一天中最早时间和最晚时间:

       LocalDateTime startTm = LocalDateTime.of(LocalDate.now(),LocalTime.MIN);//2020-11-11 00:00:00

       LocalDateTime endTm = LocalDateTime.of(LocalDate.now(),LocalTime.MAX);//2020-11-11 23:59:59

一个月第一天0点到一个月最后一天23:59:59

     DateTimeFormatter da = DateTimeFormatter.ofPattern("yyyy-MM-dd");

      LocalDateTime monthTM = LocalDateTime.of(LocalDate.parse(tm + "-01", da), LocalTime.MIN); //tm为字符串类型 2020-11

      LocalDateTime min = monthTM.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);

      LocalDateTime max = monthTM.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);

一年中第一天0点到最后一天23:59:59

      DateTimeFormatter da = DateTimeFormatter.ofPattern("yyyy-MM-dd");

      LocalDateTime monthY = LocalDateTime.of(LocalDate.parse(tm + "-01-01", da),LocalTime.MIN); //tm为字符串 2020

      LocalDateTime min = monthY.with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);

      LocalDateTime  max = monthY.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59);

      遇到一问题,当把上面的min,max转为Date类型时,ZoneId zoneId = ZoneId.systemDefault(); Instant Sinstant = min.atZone(zoneId).toInstant(); Instant Einstant = max.atZone(zoneId).toInstant(); Instant这里的值比min,max少8个小时,当转为Date时,自动补齐8个小时 Date startTime = Date.from(Sinstant); Date endTime = Date.from(Einstant); 这里的时间恢复正常,应该和ZoneId涉及的时区有关

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java中的LocalDate类是用来表示日期的类,与java.util.Date类略有不同,它只包含日期,没有时间。可以使用LocalDate类进行各种日期操作。例如,可以使用以下方法获取年份、月份、日期、星期几等信息: - `LocalDate.now().getYear()`:获取当前年份。 - `LocalDate.now().getMonthValue()`:获取当前月份的数值表示,从1开始。 - `LocalDate.now().getMonth()`:获取当前月份的英文表示。 - `LocalDate.now().getDayOfMonth()`:获取当前日期,从1开始。 - `LocalDate.now().getDayOfYear()`:获取当前日期在当年中的第几天,从1开始。 - `LocalDate.now().getDayOfWeek()`:获取当前星期几。 - `LocalDate.now().lengthOfYear()`:获取当年的天数。 - `LocalDate.now().lengthOfMonth()`:获取当月的天数。 - `LocalDate.now().toEpochDay()`:获取当前日期与时间纪元(1970年1月1日)相差的天数,负数表示在时间纪元之前多少天。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Java8时间类LocalDate常用操作](https://blog.csdn.net/weixin_42130889/article/details/114524861)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Java LocalDate](https://blog.csdn.net/qq_43308246/article/details/126142689)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值