Flag This Hub

JAVA TUTORIAL - program 2

By


Hello again

Now I will show you how to write a program that will show the current time. It has only one particular variable that you need to look at with caution. All the rest is pretty straightforward.

  import javax.swing.JOptionPane;

  public class ShowCurrentTime {

    public static void main(String[] args) 

    {

      // Obtain the total milliseconds since the midnight, Jan 1, 1970

      long totalMilliseconds = System.currentTimeMillis();

     // Obtain the total seconds since the midnight, Jan 1, 1970

     long totalSeconds = totalMilliseconds / 1000;


     // Compute the current second in the minute in the hour

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


     // Obtain the total minutes

     long totalMinutes = totalSeconds / 60;


     // Compute the current minute in the hour

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


     // Obtain the total hours

     long totalHours = totalMinutes / 60;


     // Compute the current hour

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


     // Display results

     String output = "Current time is " + currentHour + ":"

       + currentMinute + ":" + currentSecond + " GMT";


     JOptionPane.showMessageDialog(null, output);

   }

 }

Comments

No comments yet.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working