How to get the current time in seconds in Java?
Posted by Joys of Programming on in Java
Java provides functions like currentTimeMillis and nanoTime to print the current time in milliseconds and nanoseconds respectively. But to get the system time in seconds, we can make use either of these functions. The following program illustrates this
1 second = 1000 milliseconds = 1000000000 nanoseconds
class CurrentTimeSeconds {
public static void main(String args[]){
long time = System.currentTimeMillis()/1000;
System.out.println("Current Time: "+time+"s");
time = System.nanoTime()/1000000000L;
System.out.println("Current Time: "+time+"s");
}
}
The program generates the following output:
Current Time: 1259590813s Current Time: 1259590813s
Note: Both these function return the time passed since January 1, 1970.
Comments:
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=550a9be1-68a2-410c-b18e-11b44283ab41)