Keith
Keith

Reputation: 261

How to include milliseconds in a formatted date string?

Hi I am coding the following:

String sph=(String) android.text.format.DateFormat.format("yyyy-MM-dd_hh-mm-ss_SSS", new java.util.Date()); 

I want the current date and time and milliseconds

what it gives me is:

2011-09-01_09-55-03-SSS

The SSS is not converting back to milliseconds...

Does anyone know why and what should it be to get the milliseconds in 3 position?

Thanks

Upvotes: 15

Views: 25159

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 339372

tl;dr

ZonedDateTime          // Represent a moment in the wall-clock time used by the people of a certain region (a time zone).
.now()                 // Capture current moment. Better to pass optional argument for `ZoneId` (time zone). Returns a `ZonedDateTime` object.
.format(               // Generate a `String` with text in a custom formatting pattern.
    DateTimeFormatter.ofPattern( "uuuu-MM-dd_HH-mm-ss-SSS" )
)                      // Returns a `String` object.

2018-08-26_15-43-24-895

Instant

You are using troublesome old legacy date-time classes. Instead use the java.time classes.

If you want the date-time in UTC, use the Instant class. This class holds nanosecond resolution, more than enough for milliseconds.

Instant instant = Instant.now();
String output = instant.toString();

That toString method uses the DateTimeFormatter.ISO_INSTANT formatter which prints 0, 3, 6, or 9 digits in the decimal fraction, as many as needed appropriate to the actual data value.

In Java 8 the current moment is captured only up to milliseconds but a new implementation of Clock in Java 9 may capture up to nanoseconds. So truncate to milliseconds if that is your requirement. Specify your desired truncation by a TemporalUnit as implemented in ChronoUnit.MILLIS.

Instant instant = Instant.now().truncatedTo( ChronoUnit.MILLIS );

ZonedDateTime

If you want a specify time zone, apply a ZoneId to get a ZonedDateTime.

Instant instant = Instant.now();

instant.toString(): 2018-08-26T19:43:24.895621Z

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

instantTruncated.toString(): 2018-08-26T19:43:24.895Z

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( zoneId );
String output = zdt.toString();

2018-08-26T15:43:24.895621-04:00[America/Montreal]

Again if you want other formats, search Stack Overflow for DateTimeFormatter.

DateTimeFormatter

If you want to force three digits for milliseconds, even if the value is all zeros, specify a custom formatting pattern using DateTimeFormatter class.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd_HH-mm-ss-SSS" ) ;
String output = zdt.format( f ) ;

2018-08-26_15-43-24-895


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?

Upvotes: 5

Bozho
Bozho

Reputation: 597234

Try using the same format with SimpleDateFormat

Upvotes: 0

Volo
Volo

Reputation: 29438

Use the following:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");
String dateString = formatter.format(new java.util.Date());

Upvotes: 22

Related Questions