Program to display Current Greenwich Mean Time in Java !

This is an interview question that was recently asked to a student of mine in a Java Programming Interview. And I thought of sharing it with all who are following my blogpost.

Let me repeat the question once again.

Question: Write a simple program to display the current time in GMT (Greenwich Mean Time) in the format hour:minute:second, such as 14:24:7.


Have you heard of the UNIX epoch. Wait for a minute. There is a method in the System class named ‘currentTimeMillis( )‘ that returns the current time in milliseconds elapsed since the time 00:00:00 on January 1st 1970 GMT. This time is known as the UNIX epoch.

Using this method, we can obtain the current time in milliseconds and then compute the current second, minute and hour as follows.


public class DisplayCurrentTime


public static void main( String [] args )


long milliseconds = System.currentTimeMillis( ); // Will give you UNIX epoch

long seconds = milliseconds / 1000; // Will give you the total seconds

//Remember 1 second = 1000 milliseconds

long currentSecond = seconds % 60; // Will give you the current second

long minutes = seconds / 60; // Will give you the total minutes

//Remember 1 minute = 60 seconds

long currentMinute = minutes % 60; // Will give you the current minute

long hours = minutes / 60; // Will give you total hours

//Remember 1 hour = 60 minutes

long currentHour = hours % 24; // Will give you the current hour.

System.out.println( “The current time is  ” + currentHour + “:” + currentMinute + “:” + currentSecond + “GMT” );



About the Author:

Subhash.K.U, Principal Mentor,

Subhash Programming Classes, Bangalore

View My Complete Profile !

I love to teach you ! Click here !

My name is Subhash.K.U. I work as Principal Mentor at Subhash Programming Classes helping fresh engineering graduates and professionals crack their programming interviews through C, C++, Data Structures, Java and Linux Programming. 




2 Response Comments

  • Abhishek Mishra  February 22, 2016 at 7:30 pm

    Hello Sir
    I used your code to compile in my system.
    When i run this code i find error
    “unmappable character for encoding Cp1252”

    When i search on internet i find that there are two king of double quote (“) and (“).

    You have used (“) quote in your program which is not supported by ASCII Double quote character.
    I change the quotes to ( ” “).
    And the program is working.
    So kindly change the quotes in the program so that other
    learner has to not face the same issue.

    • subhash_user  February 23, 2016 at 5:35 am

      Hi Abhishek,

      Thanks for your comment. And good to know you tried this. Superb. I think, you directly copied this code onto your editor. That is the problem. Type the program and use the double quotes as supported by your editor. It will work fine.

      Your Loving Teacher !



Leave A Comment

Please enter your name. Please enter an valid email address. Please enter a message.