Untangling "efficient" code in java

N

New

Hello.

I am trying to reproduce the same results so I can prove to myself I
understand what is happening:

This prints a time in hh:mm:ss PM/AM
Date now = new Date();
out.println("DEBUG: " + DateFormat.getTimeInstance().format(now));

I am trying to expand the above to make sure I understand it:

I keep getting errors (I'm using JSP so the errors are hard to read)
so I don't think it's correct:

Date now = new Date();
DateFormat df1 = new DateFormat();
df1.getTimeInstance();
DateFormat df2 = new DateFormat();
String str = new String("");
str = df2.format(now);
out.println("DEBUG: " + str);

Where did I go wrong?

Thank you, New
 
T

Tor Iver Wilhelmsen

DateFormat df1 = new DateFormat();

You don't need this, because
df1.getTimeInstance();

is a static method that gets the current locale's default format, OR
if you have the US-oriented JRE: the American format you seem to get.

Also, you get it then immediately throw it away.
DateFormat df2 = new DateFormat();

This creates an uninitiated DateFormat.
String str = new String("");

That was unneccesary since you reassign it
str = df2.format(now);
here.

Where did I go wrong?

You never actually used the DateFormat you wanted.
 
T

Tony Morris

Date now = new Date();
DateFormat df1 = new DateFormat(); // object created and assigned to df1
reference // which is never again used (except for
the waste of time on the next line)
df1.getTimeInstance(); // should be a static call
(DateFormat.getTimeInstance()) and the
// return value is not kept anyway, useless line
DateFormat df2 = new DateFormat(); // object created and assigned to df2 reference.
String str = new String(""); // HORRIBLE abuse of the String(String)
constructor.
// Look up how Strings and object
references are handled in Java, //
particularly, the "interning" of literals.
// Assigned to an object that is never
used as str is
// reassigned on the next line.
str = df2.format(now);
out.println("DEBUG: " + str);

// This entire thing could have written as
// out.println("DEBUG: " + new DateFormat().format(new Date()));
// Your call.
 
N

New

I know it is less efficient...I was trying to explain that I need to
break it down into "less efficient" code UNTIL I understand the
"efficient" code.

Tony Morris said:
Date now = new Date();
DateFormat df1 = new DateFormat(); // object created and assigned to df1
reference // which is never again used (except for
the waste of time on the next line)
df1.getTimeInstance(); // should be a static call
(DateFormat.getTimeInstance()) and the
// return value is not kept anyway, useless line
DateFormat df2 = new DateFormat(); // object created and assigned to df2 reference.
String str = new String(""); // HORRIBLE abuse of the String(String)
constructor.
// Look up how Strings and object
references are handled in Java, //
particularly, the "interning" of literals.
// Assigned to an object that is never
used as str is
// reassigned on the next line.
str = df2.format(now);
out.println("DEBUG: " + str);

// This entire thing could have written as
// out.println("DEBUG: " + new DateFormat().format(new Date()));
// Your call.
--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software

New said:
Hello.

I am trying to reproduce the same results so I can prove to myself I
understand what is happening:

This prints a time in hh:mm:ss PM/AM
Date now = new Date();
out.println("DEBUG: " + DateFormat.getTimeInstance().format(now));

I am trying to expand the above to make sure I understand it:

I keep getting errors (I'm using JSP so the errors are hard to read)
so I don't think it's correct:

Date now = new Date();
DateFormat df1 = new DateFormat();
df1.getTimeInstance();
DateFormat df2 = new DateFormat();
String str = new String("");
str = df2.format(now);
out.println("DEBUG: " + str);

Where did I go wrong?

Thank you, New
 
A

Andrew Thompson

Tony Morris said:
.....
I know it is less efficient...I was trying to explain that I need to
break it down into "less efficient" code UNTIL I understand the
"efficient" code.

Please note that what Tony was saying above
does not constitute code that is less efficient, it
is code that does _nothing_. Big difference.

Try and understand what Tony was saying..
(There are some important points there)

And could I ask that you put your comments
_after_ what you are commenting on, rather
than before? It (top-posting) makes converstaions
hard to understand.
 
O

Oscar Kind

New said:
I am trying to reproduce the same results so I can prove to myself I
understand what is happening:

This prints a time in hh:mm:ss PM/AM
Date now = new Date();
out.println("DEBUG: " + DateFormat.getTimeInstance().format(now));

I am trying to expand the above to make sure I understand it:
Where did I go wrong?

You chose a complicated way to break it down, using too many objects.
This resulted, as others have pointed out, in errors. The result is even
more difficult to understand.

Personally, I'd break the code down as follows, using comments and empty
lines. Note I also added a few extra options for the (time) format
(commented out).

// Get the current time.
Date now = new Date();

// Get the default time format for the default locale.
DateFormat timeFormat = DateFormat.getTimeInstance();

// (commented out) Get the full time format for the default locale.
//DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.FULL);

// (commented out) Get the short time format for The Netherlands.
//Locale theNetherlands = new Locale("nl", "NL");
//DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT,
// theNetherlands);

// Format the current time using the time format above.
String formattedTime = timeFormat.format(now);

// Debug log.
out.println("DEBUG: " + formattedTime);


Oscar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top