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.
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()); } } }
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?
ReplyDeletethank you @blacksaibot for suggestion have updated the code. Please point out improvements
ReplyDelete