• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Formatting long

 
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Chris Baron
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output is seconds, separator, 1st millissecond-digit.

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


Me neither
cb
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about PrintStream.printf()? It can take a local as well as a format string.

Cheers,
[ October 24, 2005: Message edited by: Tom Blough ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Seb Mathe
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I'm now sure you should use DecimalFormat instead of DateFormat !!
And format your number, not a Date...
 
Chris Baron
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
divide it by 1000.0 or 1000d
 
Chris Baron
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aah, thanks, Seb.
I've should have asked earlier
cb
 
Chris Baron
Ranch Hand
Posts: 1061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FYI,
or for the case someone searchs for this, that's what it's looking now:
Any proposals for improvement?
cb
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about calling System.currentTimeMills() instead of new Date().getTime() ?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic