Java时间系列(JDK8)--LocalDateTime的使用

原文网址:Java时间系列(JDK8)--LocalDateTime的使用_IT利刃出鞘的博客-CSDN博客

简介

说明

本文用示例来介绍LocalDateTime的用法。

相关网址

LocalDate--使用/教程/实例_IT利刃出鞘的博客-CSDN博客

Java--Period/Duration--使用/教程/实例_IT利刃出鞘的博客-CSDN博客

简述

从Java 8开始,java.time包提供了新的日期和时间API,主要涉及的类型有:

  • 本地日期和时间:LocalDateTime,LocalDate,LocalTime;
  • 带时区的日期和时间:ZonedDateTime;
  • 时刻:Instant;
  • 时区:ZoneId,ZoneOffset;
  • 时间间隔:Period, Duration。两个类表示两个日期或时间量之间的差
  • DateTimeFormatter取代SimpleDateFormat。

        和旧API相比,新API严格区分时刻、本地日期、本地时间和带时区的日期时间,对日期和时间运算更加方便。
        此外,新API修正了旧API不合理的常量设计:

  • Month的范围用1~12表示1月到12月;
  • Week的范围用1~7表示周一到周日。

最后,新API的类型全部是不变类型(和String类似),可以放心使用不必担心被修改。

对应的SQL的类型

SQL -> Java

date -> LocalDate
time -> LocalTime
timestamp -> LocalDateTime

常用操作

注意:以下所有用法对于LocalDate和LocalTime是类似的。

创建

根据年、月、日、时、分、秒、纳秒等创建LocalDateTime

eg:
LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00
LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00
LocalTime now = LocalTime.now(); // 23:11:08.006

all method
LocalDateTime of(LocalDate date, LocalTime time)
LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute)
LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)
LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)
LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)
LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)

与Date转换

Date转换为LocalDateTime

Date date = new Date();
LocalDateTime nowDteTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

Date所在包:java.util.Date 

LocalDateTime转换为Date

LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault());
Date date = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());

与LocalDate/LocalTime转换

上边是文章的部分内容,为便于维护,全文已转移到此网址:Java时间(JDK8)-LocalDateTime的使用 - 自学精灵

  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
mybatis-plus在使用jdk8的LocalDateTime查询时,需要添加一个配置项。在mybatis-plus的配置文件中添加以下配置: ```yaml mybatis-plus: configuration: # 查询返回null值时,不会映射到实体类上 map-underscore-to-camel-case: true # 配置 LocalDateTime 和 Date 的序列化和反序列化方式 # 使用 jdk8 的时间类型,需要配置 # 下面两个配置可以根据自己的需要进行配置 # LocalDateTime 序列化方式 # 可选值:java.util.Date、java.sql.Date # 默认值:java.util.Date # type-handle: java.sql.Date type-handlers-package: com.baomidou.mybatisplus.extension.handlers ``` 在以上配置中,需要注意的是 `type-handlers-package` 配置项。它指定了mybatis-plus的类型处理器包路径,其中包含有针对jdk8的LocalDateTime类型的处理器。 另外,如果在实体类中使用jdk8的LocalDateTime类型,需要在对应的mapper.xml文件中,将数据库中的datetime类型字段映射为LocalDateTime类型,例如: ```xml <resultMap id="BaseResultMap" type="com.example.entity.User"> <result column="gmt_create" property="gmtCreate" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/> <result column="gmt_modified" property="gmtModified" jdbcType="TIMESTAMP" javaType="java.time.LocalDateTime"/> </resultMap> <select id="getUserById" resultMap="BaseResultMap"> select id, name, age, gmt_create, gmt_modified from user where id = #{id} </select> ``` 这样就可以在mybatis-plus中使用jdk8的LocalDateTime类型进行查询了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT利刃出鞘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值