Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

java.util.date - How to remove milliseconds from Date Object format in Java

Since the java.util.Date object stores Date as 2014-01-24 17:33:47.214, but I want the Date format as 2014-01-24 17:33:47. I want to remove the milliseconds part.

I checked a question related to my question...

How to remove sub seconds part of Date object

I've tried the given answer

long time = date.getTime();
date.setTime((time / 1000) * 1000);

but I've got my result Date format as 2014-01-24 17:33:47.0. How can I remove that 0 from my Date format???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

tl;dr

Lop off the fractional second.

myJavaUtilDate.toInstant()                         // Convert from legacy class to modern class. Returns a `Instant` object.
              .truncatedTo( ChronoUnit.SECONDS )   // Generate new `Instant` object based on the values of the original, but chopping off the fraction-of-second.

Hide the fractional second, when generating a String.

myJavaUtilDate.toInstant()                         // Convert from legacy class to modern class. Returns a `Instant` object.
              .atOffset( ZoneOffset.UTC )          // Return a `OffsetDateTime` object. 
              .format( DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" ) ). // Ask the `OffsetDateTime` object to generate a `String` with text representing its value, in a format defined in the `DateTimeFormatter` object.

Avoid legacy date-time classes

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.

Instant

Convert your old java.util.Date object to a java.time.Instant by calling new method added to the old class.

Instant instant = myJavaUtilDate.toInstant() ;

Table of types of date-time classes in modern java.time versus legacy.

Truncate

If you want to change value of the data itself to drop the fraction of a second, you can truncate. The java.time classes use immutable objects, so we generate a new object rather than alter (mutate) the original.

Instant instantTruncated = instant.truncatedTo( ChronoUnit.SECONDS );

Generating string

If instead of truncating you merely want to suppress the display of the fractional seconds when generating a string representing the date-time value, define a formatter to suit your needs.

For example, "uuuu-MM-dd HH:mm:ss" makes no mention of a fractional second, so any milliseconds contained in the data simply does not appear in the generated string.

Convert Instant to a OffsetDateTime for more flexible formatting.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd HH:mm:ss" );
OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC )
String output = odt.format( f );

Time zone

Note that your Question ignores the issue of time zone. If you intended to use UTC, the above code works as both Date and Instant are in UTC by definition. If instead you want to perceive the given data through the lens of some region’s wall-clock time, apply a time zone. Search Stack Overflow for ZoneId and ZonedDateTime class names for much more info.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...