LocalDateTime删除毫秒 [英] LocalDateTime remove the milliseconds

查看:2268
本文介绍了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删除毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆