Easy practice with
currentTimeMillis method and
more examples
Java programming
Nooria Esmaelzade
2016
What do you know about (GMT)??
GMT is World Time and
the basis of every
world time zone
Program1
Write a program that displays current time in GMT in the format
hour:minute:second
To create the program we use the currentTimeMillis method in the System class
public class ShowCurrentTime {
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 = (int)(totalSeconds % 60);
// Obtain the total minutes
long totalMinutes = totalSeconds / 60;
// Compute the current minute in the hour
long currentMinute = (int)(totalMinutes % 60);
// Obtain the total hours
long totalHours = totalMinutes / 60;
// Compute the current hour
long currentHour = (int)(totalHours % 24);
// Display results
System.out.println("Current time is " + currentHour + ":"
+ currentMinute + ":" + currentSecond + " GMT");
}
}
Out
put
Current time is 2:13:6 GMT
Program 2
Keeping Two Digits After Decimal Points
public class show2Digit {
public static void main(String[ ]args) {
double purchaseAmount = 3.14;
double tax = purchaseAmount * 10.3;
System.out.println((int)(tax * 100)/100.0);
}
}
Output 32.34
Computing Loan Payments
The program asks the user to enter:
- Loan amount
- Annually interest rate
- Number of years
Then the program should compute:
- The monthly interest rate
- Monthly payment
- The total payment
12
)1(
11 



arsnumberOfYe
erestRatemonthlyInt
erestRatemonthlyIntloanAmount
import java.util.Scanner;
public class ComputeLoan {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Enter yearly interest rate
System.out.print("Enter yearly interest rate, for example 8.25: ");
double annualInterestRate = input.nextDouble();
// Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
// Enter number of years
System.out.print(
"Enter number of years as an integer, for example 5: ");
int numberOfYears = input.nextInt();
Program 3
Computing Loan Payments
Program 3 continuous
Computing Loan Payments
// Enter loan amount
System.out.print("Enter loan amount, for example 120000.95: ");
double loanAmount = input.nextDouble();
// Calculate payment
double monthlyPayment = loanAmount * monthlyInterestRate / (1
- 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
// Display results
System.out.println("The monthly payment is " +
(int)(monthlyPayment * 100) / 100.0);
System.out.println("The total payment is " +
(int)(totalPayment * 100) / 100.0);
}
}
Enter yearly interest rate, for example 8.25: 7
Enter number of years as an integer, for example 5: 3
Enter loan amount, for example 120000.95: 100000
The monthly payment is 3087.7
The total payment is 111157.54
Out put
Math.pow method
The java.lang.Math.pow(double a, double b) returns the value of the
first argument raised to the power of the second argument

Easy practice with current timemillis method

  • 1.
    Easy practice with currentTimeMillismethod and more examples Java programming Nooria Esmaelzade 2016
  • 2.
    What do youknow about (GMT)?? GMT is World Time and the basis of every world time zone
  • 3.
    Program1 Write a programthat displays current time in GMT in the format hour:minute:second To create the program we use the currentTimeMillis method in the System class
  • 4.
    public class ShowCurrentTime{ 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 = (int)(totalSeconds % 60); // Obtain the total minutes long totalMinutes = totalSeconds / 60; // Compute the current minute in the hour long currentMinute = (int)(totalMinutes % 60); // Obtain the total hours long totalHours = totalMinutes / 60; // Compute the current hour long currentHour = (int)(totalHours % 24); // Display results System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT"); } } Out put Current time is 2:13:6 GMT
  • 5.
    Program 2 Keeping TwoDigits After Decimal Points public class show2Digit { public static void main(String[ ]args) { double purchaseAmount = 3.14; double tax = purchaseAmount * 10.3; System.out.println((int)(tax * 100)/100.0); } } Output 32.34
  • 6.
    Computing Loan Payments Theprogram asks the user to enter: - Loan amount - Annually interest rate - Number of years Then the program should compute: - The monthly interest rate - Monthly payment - The total payment 12 )1( 11     arsnumberOfYe erestRatemonthlyInt erestRatemonthlyIntloanAmount
  • 7.
    import java.util.Scanner; public classComputeLoan { public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Enter yearly interest rate System.out.print("Enter yearly interest rate, for example 8.25: "); double annualInterestRate = input.nextDouble(); // Obtain monthly interest rate double monthlyInterestRate = annualInterestRate / 1200; // Enter number of years System.out.print( "Enter number of years as an integer, for example 5: "); int numberOfYears = input.nextInt(); Program 3 Computing Loan Payments
  • 8.
    Program 3 continuous ComputingLoan Payments // Enter loan amount System.out.print("Enter loan amount, for example 120000.95: "); double loanAmount = input.nextDouble(); // Calculate payment double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); double totalPayment = monthlyPayment * numberOfYears * 12; // Display results System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) / 100.0); System.out.println("The total payment is " + (int)(totalPayment * 100) / 100.0); } } Enter yearly interest rate, for example 8.25: 7 Enter number of years as an integer, for example 5: 3 Enter loan amount, for example 120000.95: 100000 The monthly payment is 3087.7 The total payment is 111157.54 Out put
  • 9.
    Math.pow method The java.lang.Math.pow(doublea, double b) returns the value of the first argument raised to the power of the second argument