This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, i want print out the execution-time of my program. With the country-specific decimal separator and one decimal digit. I finally got a solution, but i have that feeling, that i wasted time and code . How to make it better?
thx cb [ October 24, 2005: Message edited by: Chris Baron ]
DateFormat is supposed to be used to format date and time information. For example, it let's you format Strings like
25 December 2005
24 September 2005 08:25:00
Note that these are all absolute date and time. If you want to print a duration instead, you should just print the number calculated by the difference of the end time and the start time. Note that getTime() returns the value in milliseconds. Do you want to convert this into seconds? Unfortunately, there is not a built-in method to do this. So what steps do you need to take in order to convert milliseconds into seconds? Perhaps you should try describing this in natural language (i.e. English, etc.) before you convert it into Java.
long delta = new Date().getTime() - time; //time=starttime;
how do i divide it by 1000 and receive a decimal value? Thx cb
Originally posted by Seb Mathe divide it by 1000.0 or 1000d
There is one caveat to Seb's proposed solution: floating point arithmetic is inexact and DecimalFormat.format() does not do any rounding. If you want to make a more pendantic solution to your problem, you can use integer division and modulus (%) to create an exact decimal representation as a String. The exact details are left as an exercise for the reader.
Joined: Mar 21, 2003
Thanks for your hints. The millisecond output doesn't have to be pedantic correct, but Layne's way with int division and modulo shows the results always with the separator and the decimal digit. Also if it is zero.