Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 603
Keith Avatar asked Sep 01 '11 17:09

Keith


People also ask

How do you add milliseconds to a date?

AddMilliseconds() Method in C# This method is used to return a new DateTime that adds the specified number of milliseconds to the value of this instance. Syntax: public DateTime AddMilliseconds (double value);

How do you write milliseconds in time format?

You can add milliseconds by adding SSS at the end, such as the format will be HH:mm:ss. SSS .


2 Answers

Use the following:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");
String dateString = formatter.format(new java.util.Date());
like image 83
Idolon Avatar answered Oct 20 '22 22:10

Idolon


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?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
like image 30
Basil Bourque Avatar answered Oct 20 '22 22:10

Basil Bourque