Convert given Time to nanosecond precision using java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Convert given Time to nanosecond precision using java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

my code:

String timeColonPattern = "HH:mm:ss.SSSSSSSSS";             
DateTimeFormatter timeColonFormatter = DateTimeFormatter.ofPattern(timeColonPattern);
LocalTime colonTime = LocalTime.parse("09:30:23.187235");

I want “09:30:23.187235” to be converted into nanosecond precision. colonTime is same as 09:30:23.187235 but I want it to be converted into 09:30:23.187235000. i.e. It should add zeros if the digits after decimal is less than 9. Any suggestion how can I achieve that?

Answer

LocalTime already has nanosecond precision. If you want to have this precision being output, do not use the .SSSSSSSSS pattern, but use .nnnnnnnnn:

LocalTime colonTime = LocalTime.parse("09:30:23.187235");
System.out.println(
    DateTimeFormatter.ofPattern("HH:mm:ss.nnnnnnnnn")
                     .format(colonTime)
);

See this code run in IdeOne.com.

09:30:23.187235000

See all possible patterns see the DateTimeFormatter class.

We are here to answer your question about Convert given Time to nanosecond precision using java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji