|
|
|
Let n = 15432 seconds
1. We can find the # seconds as follows:
257 <---- # minutes
-------------
60 / 15432
15420
--------
12 <--- # seconds
So: 15432 seconds = 257 minutes + 12 seconds
Or: 15432 seconds = 15432 / 60 minutes + 15432 % 60 seconds
|
input n; // n = total number of seconds
seconds = n % 60; // computes seconds
n = n / 60; // n is now = remaining minutes
minutes = n % 60; // computes minutes
n = n / 60; // n is now = remaining hours
hours = n;
|
n = 15432; hours = 15432 / 3600 = 4 r = 15432 % 3600 = 1032 minutes = 1032 / 60 = 17 seconds = 1032 % 60 = 12 |
import java.util.Scanner;
public class Hours
{
public static void main(String[] args)
{
int n, r, hours, minutes, seconds;
Scanner in = new Scanner(System.in);
System.out.print("Enter # seconds: ");
n = in.nextInt();
// in.nextInt() reads in an integer value
seconds = n % 60; // computes seconds
n = n / 60; // n is now = remaining minutes
minutes = n % 60; // computes minutes
n = n / 60; // n is now = remaining hours
hours = n;
System.out.print(n);
System.out.print(" seconds = ");
System.out.print(hours);
System.out.print(" hours + ");
System.out.print(minutes);
System.out.print(" minutes+ ");
System.out.print(seconds);
System.out.println(" seconds.");
}
}
|
How to run the program:
|
|
|
Let n = 100000000 millisec
1. Find total number of seconds since midnight, Jan 1 1970:
n = n / 1000 = 100000000 / 1000 = 100000 (total # seconds)
2. Find seconds in current time:
seconds = n % 60 = 100000000 % 1000 = 40 *** part of answer
n = n / 60 = 100000 / 60 = 1666 (total # minutes)
3. Find minutes in current time:
minutes = n % 60 = 1666 / 60 = 46 *** part of answer
n = n / 60 = 1666 % 60 = 27 (total # hours)
3. Find hours in current time:
hours = n % 24 = 27 % 24 = 3 *** part of answer
|
n = System.currentTimeMillis(); // n = total # milli sec n = n / 1000; // n is now = total # sec seconds = n % 60; // Compute seconds in current time n = n / 60; // n is now = total # minutes minutes = n % 60; // Compute minutes in current time n = n / 60; // n is now = total # hours hours = n % 24; // Compute hours in current time |
public class ShowCurrentTime
{
public static void main(String[] args)
{
long nMillis; // We need long for accuracy !!!
long n, hours, minutes, seconds;
nMillis = System.currentTimeMillis();
n = nMillis/1000; // Total # Second;
seconds = n % 60; // Seconds
n = n / 60; // Total # minutes;
minutes = n % 60; // Minutes
n = n / 60; // Total # hours;
hours = n % 24; // Hours
System.out.print(hours);
System.out.print(":");
System.out.print(minutes);
System.out.print(":");
System.out.print(seconds);
System.out.println(" GMT");
}
}
|
How to run the program:
|