Home > OS >  How to format a date without losing any microsecond
How to format a date without losing any microsecond

Time:01-11

Good morning. Do you know a format for this kind of date without losing the last 3 numbers ?

"2022-09-23-11.25.52.660135"

I tried to use this formatter

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SS")

but I lose the "135" and I get 2022-09-23-11.25.52.660.

I tried other combinations too but still i can't get the full date .

CodePudding user response:

Not only I do not know of any such format, but there exists no such format, because:

  • the available formats are listed exhaustively in the documentation of the class, and no such format is included in that list. If someone knew of some format which magically works even though it is not listed, you should not use it because this would constitute documentation by divine revelation and programming by magic.

  • both SimpleDateFormat and DateFormat were built to work with Date, which is defined by its documentation as having millisecond precision. Thus, there is no way to format microseconds because Date does not have microseconds.

In light of the above, your statement that you 'lose the "135"' is suspicious: what makes you believe that you had a 135 in the first place? Come to think of it, you have completely failed to explain something very important: are you trying to print a date, or to parse a date? If you are trying to parse a date, then what makes you believe that you can have that 135 in a date? That's why showing your code is always very important on StackOverflow.

To answer the "how do you do it" part:

  • If you really like SimpleDateFormat, you can use it to output the time coordinate without microseconds, then append a '.' to it, and then convert the microseconds to string by yourself, left-padding with zeros.

  • However, if you are using Instant, then you should be using the DateTimeFormatter class, which offers a format specifier for describing fractions of a second with precisions greater than milliseconds.

CodePudding user response:

In the Java programming language, the java.util.Date class represents a specific instant in time, with millisecond precision. The Date class also provides methods for formatting and parsing date strings, including the representation of the microseconds.

The SimpleDateFormat class is used to format and parse date strings in Java. The class provides a set of formatting patterns that can be used to represent different parts of a date and time value.

To represent the last 3 digits of microseconds in a date format, you can use the pattern SSS in your format string. The S character is used to represent milliseconds, and the number of S characters in the pattern determines the number of digits of the milliseconds value to include in the output. So SSS would include the last 3 digits of microseconds.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

String formattedDate = format.format(new Date());

You can also use the SSSSSS to have a 6 digit microsecond representation.

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");

String formattedDate = format.format(new Date());

It is worth noting that the Date class does not store information about the microseconds, it only stores the time in milliseconds, which means the microsecond representation is only for the formatting.

  • Related