0

Is there any way to display time upto microseconds precision using LocalDateTime in java? Like we used in SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:S");

Similarly, what should I do if I want to display like this: hours:minutes:seconds:milliseconds:microseconds I read it can be done using LocalDateTime. But how?

Ammarah
  • 43
  • 10

4 Answers4

0

Looks like java.time.format.DateTimeFormatter will do you what you want. See the javadoc here: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

The codes appear to be the same as they were with SimpleDateFormat, so the same format string "HH:mm:ss:S" will continue to work.

neuronaut
  • 2,689
  • 18
  • 24
  • if I want to display microseconds, "HH:mm:ss:S - this is upto milliseconds only", how microseconds will be displayed? – Ammarah Sep 07 '16 at 23:54
  • No, the formatting pattern codes in `DateTimeFormatter` are similar to the codes in `SimpleDateFormat` but not exactly the same. – Basil Bourque Sep 08 '16 at 04:14
0

Last I read, java 8 can do nanoseconds as per documentation here... https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html - I heard java 9 will have some major updates to date and time features.

John1776
  • 58
  • 6
0

Java7

If you use a Timestamp Object then you can use the normal formatting for the Date part and then append ":" + timestamp.getNanos (); to get a nano second formatting String

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

ISO 8601

Your desired format for a time-of-day is standard ISO 8601 format. The java.time classes use ISO 8601 formats by default when parsing/generating String representations of their date-time values.

Nanoseconds

The java.time classes support a resolution up to nanoseconds. The legacy date-time classes they supplant were limited to milliseconds.

LocalTime

The LocalTime class represents a time-of-day without a date and without a time zone.

Determining the current time-of-day does require a time zone. For any given moment, the time-of-day varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );
LocalTime lt = LocalTime.now( z );

Generate a String by calling toString.

String output = lt.toString();  // 12:34:56.789

In Java 8, capturing the current time is limited to milliseconds. The class can hold up to nanoseconds, but the Clock implementation has legacy limitations. In Java 9, a new Clock implementation is capable of capturing the current time in up to nanoseconds (depending on the host computer’s hardware capability).

The Question mentions LocalDateTime. That class is for a date-time, a date plus a time-of-day. But the class purposely lacks any concept of time zone. Almost never what you want, as a LocalDateTime does not represent a moment on the timeline. Without the context of a time zone saying "3 PM on Jan 15th this year" has no meaning as 3 PM in Paris Texas is not the same as in Montréal Québec which is not the same as in Auckland New Zealand. Assign a time zone (ZoneId) to determine an actual moment in a ZonedDateTime object. For UTC values, just use Instant.

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154