ZonedDateTime 与 Timestamp之间的转化

import java.sql.Timestamp;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter(autoApply = true)
public final class JodaDateTimeConverter implements AttributeConverter<ZonedDateTime, Timestamp> {

    @Override
    public Timestamp convertToDatabaseColumn(ZonedDateTime dateTime) {
        return dateTime == null ? null : Timestamp.from(dateTime.toInstant());
    }

    @Override
    public ZonedDateTime convertToEntityAttribute(Timestamp timestamp) {
        return timestamp == null ? null : ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneOffset.UTC);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ZonedDateTime是Java 8中提供的一个类,用于表示带有时区的日期和时间。它提供了一些方法来获取当前日期时间、从指定的时钟或时区中获取日期时间、以及根据年、月、日、小时、分钟、秒、纳秒等参数来创建ZonedDateTime实例。其中,now()方法可以从默认时区中的系统时钟中获取当前日期时间,now(Clock clock)方法可以从指定的时钟中获取当前日期时间,now(ZoneId zone)方法可以从指定时区中的系统时钟中获取当前日期时间。 此外,ZonedDateTime还提供了of()方法,可以从年、月、日、小时、分钟、秒、纳秒和时区参数来创建ZonedDateTime实例,也可以从本地日期和时间参数来创建ZonedDateTime实例。还可以使用parse(CharSequence text)方法来从一个文本字符串中解析出ZonedDateTime实例,或者使用parse(CharSequence text, DateTimeFormatter formatter)方法来从使用特定格式的文本字符串中解析出ZonedDateTime实例。 最后,ZonedDateTime还提供了withZoneSameInstant(ZoneId zone)方法,可以返回此日期时间在不同时区下的副本,保留即时。 总结起来,ZonedDateTime是用于表示带有时区的日期和时间的类,它提供了一系列方法来获取当前日期时间、创建ZonedDateTime实例以及在不同时区下进行操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ZonedDateTime类](https://blog.csdn.net/Window_mouse/article/details/116356788)[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: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值