Pages

Tuesday, 30 August 2016

Chapter 9 Exercise 3, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

9.3 (Use the Date class) Write a program that creates a Date object, sets its elapsed
time to 10000 , 100000 , 1000000 , 10000000 , 100000000 , 1000000000 ,
10000000000 , and 100000000000 , and displays the date and time using the
toString() method, respectively.

import java.util.Date;

public class Exercise_03 {

    public static void main(String[] args) {
        Date date;
        long time = 10000;
        for (int i = 0; i < 8; i++, time *= 10) {
            date = new Date(time);
            System.out.println(date.toString());
        }
    }
}

2 comments :

  1. This is horrible code. You're seriously creating a new Date instance each time you go through that loop? Why not just create ONE object and set the time?

    ReplyDelete
  2. thank you @blacksaibot for suggestion have updated the code. Please point out improvements

    ReplyDelete