LocalDateTime删除毫秒

编程入门 行业动态 更新时间:2024-10-11 09:23:29
本文介绍了LocalDateTime删除毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Java 8的Java应用程序中工作.

I am working in java application where i am using the Java 8.

我已经集成了数据库(多个数据库Oracle,Mysql,Postgres),并且在数据库中我在其中创建日期的字符串.

I have integrated the database(multiple database Oracle,Mysql,Postgres) and where in DB i string the created date.

数据库中的日期格式为-2015-07-29 16:23:28.143

the date format in DB is - 2015-07-29 16:23:28.143

我从数据库中获取了此内容,并在Localdatetime对象中进行了设置

I fetch this from DB and set in Localdatetime object

myObj.setCreated(rs.getTimestamp("created").toLocalDateTime());

所以这里的问题是我不想在响应中显示/发送毫秒.我想显示/发送日期,例如2015-07-29 16:23:28

So here the issue is i dont want to show/send the millisecond in the response. i want to show/send date like 2015-07-29 16:23:28

我尝试了格式化程序,但由于它给出字符串而失败,并且我不想将LocalDateTime对象更改为String,因为这会导致所有Java应用程序发生重大变化.因此想找到解决方法

I tried the formatter but it fails as it giving string and i dont want change the LocalDateTime object to String as this going to cause major change in all Java application.So want to find the solution

有人知道这方面的解决方案吗?

Can anybody know any solution on this.

推荐答案

只需将它们设置为0:

myObj.setCreated(rs.getTimestamp("created").toLocalDateTime().withNano(0));

样品/证明:

import java.time.LocalDateTime; public class DateTimeSample { public static void main(String[] args) { LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt); System.out.println(ldt.withNano(0)); } }

输出:

2015-07-30T16:29:11.684 2015-07-30T16:29:11

作者注:尽管这是公认的,但彼得·劳瑞(Peter Lawrey)的答案是恕我直言的,因为它使意图更加明确.


Author's note: Although this is the accepted one, Peter Lawrey's answer is IMHO preferrable because it makes the intention more clear.

更多推荐

LocalDateTime删除毫秒

本文发布于:2023-10-29 00:55:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LocalDateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!