Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Date Formatter  RSS feed

 
Rimjhim Ray
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following piece of code

newValue = formatter.parse(new String("").trim(), new Pa
rsePosition(0));

returns null in java 1.4 but returns unix epoch time in java 1.3. Can any of you confirm if there has been such a major change in implementation of SDF from 1.3 to 1.4? If it has where can I find the documentation?

Cheers
Rimjhim
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, note that

new String("").trim()

is the same as

new String("")

which is the same as

""

Second: I don't see anywhere in the Javadocs where it says that "" will be handled specially; it's not a valid date, so I'd expect null to be returned, regardless of JDK version. I think this code was relying on undefined behavior.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There seems to be a resurgence of "new String("blah")"...

From the API:

public String(String original)

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!