wood burning stoves 2.0*
The moose likes Java in General and the fly likes Formatting long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Formatting long" Watch "Formatting long" New topic
Author

Formatting long

Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
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 ]
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
First of all, do you to print the time in milliseconds ? in seconds ? hours ?...

I'm not sure you should use DateFormat, instead of DecimalFormat...


Regards,<br />Seb<br /> <br />SCJP 1.4
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
The output is seconds, separator, 1st millissecond-digit.
I'm not sure you should use DateFormat, instead of DecimalFormat...

Me neither
cb
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
How about PrintStream.printf()? It can take a local as well as a format string.

Cheers,
[ October 24, 2005: Message edited by: Tom Blough ]

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
DateFormat is supposed to be used to format date and time information. For example, it let's you format Strings like

25 December 2005

or

9/11/2001

or

08:00 am

or

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.

Layne


Java API Documentation
The Java Tutorial
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225


I'm now sure you should use DecimalFormat instead of DateFormat !!
And format your number, not a Date...
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
Ok,
i have:

long delta = new Date().getTime() - time; //time=starttime;

how do i divide it by 1000 and receive a decimal value?
Thx cb
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
divide it by 1000.0 or 1000d
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
Aah, thanks, Seb.
I've should have asked earlier
cb
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
FYI,
or for the case someone searchs for this, that's what it's looking now:
Any proposals for improvement?
cb
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

How about calling System.currentTimeMills() instead of new Date().getTime() ?


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Chris Baron:
Ok,
i have:

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.

Layne
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
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.


Is it reduced to the max now?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting long