Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocalDateTime remove the milliseconds

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

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

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

I fetch this from DB and set in Localdatetime object

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

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

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.

like image 320
Nitin Murlidhar Gaikwad Avatar asked Jul 30 '15 14:07

Nitin Murlidhar Gaikwad


People also ask

How do I get rid of LocalDateTime seconds?

Make sure that you don't need seconds. In case you have 00 seconds in your LocalDateTime 2015-07-30T16:29:00.684 and you cut withNano(0) or truncatedTo(ChronoUnit. SECONDS) you will get 2015-07-30T16:29 . Sometimes this is not the result that you expected.

How do I remove milliseconds from a timestamp?

Use the setSeconds() method to remove the seconds and milliseconds from a date, e.g. date. setSeconds(0, 0) . The setSeconds method takes the seconds and milliseconds as parameters and sets the provided values on the date.


1 Answers

Truncate

You can drop anything less than seconds. Call LocalDateTime::truncatedTo.

ldt = ldt.truncatedTo(ChronoUnit.SECONDS); 
like image 138
Peter Lawrey Avatar answered Sep 18 '22 03:09

Peter Lawrey