Friday, September 11, 2009

Displaying the Current Time - Java code

This is one of the best programs I've read so far. Its a simple program but the logic is awesome. The currentTimeMillis method in the System class returns the current time in millisecond elapsed since the time 00:00:00 in January 1970 when Unix was formally launched. It is called as 'Unix epoch'.
[This program is from Java programming by Daniel liang.]

public class ShowCurrentTime
{
public static void main(String args[])
{
long totalMilliseconds=System.currentTimeMillis();
long totalSeconds=totalMillisecond/1000;

int currentSecond=(int)(totalSeconds%60);

long totalMinutes=totalSeconds/60;

int currentMinute=(int)(totalMinutes%60);

long totalHours=totalMinutes/60;

int currentHour=(int)(totalHours%24);

System.out.println("Current time is:"+currentHour+""+currentMinute+""+currentSecond+" "GMT");
}
}

No comments:

Post a Comment