Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How to convert java nanoseconds to seconds using the TimeUnit enum? Ask Question

Asked by Brian Kennedy Jun 21, 2021 1.0K views 1 answer
Share

About this question

How to convert a value from nanoseconds to seconds?

Here's the code segment:

import java.io.*;
import java.util.concurrent.*; 
..
class Stamper { 
public static void main (String[] args) { 
long start = System.nanoTime(); 
//some try with nested loops 
long end = System.nanoTime(); 
long elapsedTime = end - start;
System.out.println("elapsed: " + elapsedTime + "nano secondsn");
//convert to seconds 
TimeUnit seconds = new TimeUnit(); 
System.out.println("which is " + seconds.toSeconds(elapsedTime) + " seconds"); 
}}

The error is

Stamper.java:16:  enum types may not be instantiated.

What does this mean?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.