我正在使用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删除毫秒
发布评论