How To Include Milliseconds In A Formatted Date String?
Solution 1:
Use the following:
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");
StringdateString= formatter.format(newjava.util.Date());
Solution 2:
tl;dr
ZonedDateTime // Represent a moment in the wall-clock time used by the people of a certain region (atime 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.
Instantinstant= Instant.now();
Stringoutput= 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
.
Instantinstant= Instant.now().truncatedTo( ChronoUnit.MILLIS );
ZonedDateTime
If you want a specify time zone, apply a ZoneId
to get a ZonedDateTime
.
Instantinstant= Instant.now();
instant.toString(): 2018-08-26T19:43:24.895621Z
InstantinstantTruncated= instant.truncatedTo( ChronoUnit.MILLIS );
instantTruncated.toString(): 2018-08-26T19:43:24.895Z
ZoneIdzoneId= ZoneId.of( "America/Montreal" );
ZonedDateTimezdt= instant.atZone( zoneId );
Stringoutput= 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.
DateTimeFormatterf= DateTimeFormatter.ofPattern( "uuuu-MM-dd_HH-mm-ss-SSS" ) ;
Stringoutput= 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….
Solution 3:
Try using the same format with SimpleDateFormat
Post a Comment for "How To Include Milliseconds In A Formatted Date String?"