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
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);
You can add milliseconds by adding SSS at the end, such as the format will be HH:mm:ss. SSS .
Use the following:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss_SSS");
String dateString = formatter.format(new java.util.Date());
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With