Java long type calculation

Question

We would like to calculate the current time from milliseconds.

System.currentTimeMillis() returns the current time.

public class Main {
  public static void main(String[] args) {
    // Obtain the total milliseconds since midnight, Jan 1, 1970 GMT
    long totalMilliseconds = System.currentTimeMillis();

    //your code here

    // Display results
    System.out.println("Current time is " + currentHour + ":"
      + currentMinute + ":" + currentSecond + " GMT");
  }/*from w ww  .  j  a v a2s  .  c  om*/
}


public class Main {
  public static void main(String[] args) {
    // Obtain the total milliseconds since midnight, Jan 1, 1970
    long totalMilliseconds = System.currentTimeMillis();

    // Obtain the total seconds since midnight, Jan 1, 1970
    long totalSeconds = totalMilliseconds / 1000;

    // Compute the current second in the minute in the hour
    long currentSecond = totalSeconds % 60;

    // Obtain the total minutes
    long totalMinutes = totalSeconds / 60;

    // Compute the current minute in the hour
    long currentMinute = totalMinutes % 60;

    // Obtain the total hours
    long totalHours = totalMinutes / 60;

    // Compute the current hour
    long currentHour = totalHours % 24;

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



PreviousNext

Related