Question

Is the below statement valid in Java 7?

Timestamp.valueOf("0000-00-00 00:00:00.000000");

because building the above code with JDK 1.6 works just fine but while doing the same with JDK 1.7 I am getting:

Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

Was it helpful?

Solution

Well it's syntactically valid Java code, but I wouldn't expect it to work at execution time:

  • 0 isn't a valid month
  • 0 isn't a valid day-of-month
  • 0 may or may not be a valid year, depending on how you're counting things. (It appears to work with JDK 7, but I wouldn't use it myself.)

I'd use "0001-01-01 00:00:00.000000" - which doesn't throw an exception. That's if you really, really need such a thing, of course - if this is a magic value to use in absence of "real" data, perhaps you need a nullable column instead?

OTHER TIPS

The specification of the Timestamp class has changed between Java 6 and Java 7.

From the Javadoc for the valueof method (Java 7 version)

Throws:

IllegalArgumentException - if the given argument does not have the format yyyy-[m]m-[d]d hh:mm:ss[.f...]

But this sentence is missing from the Java 6 Javadoc. I take that to mean that the Java 6 Timestamp class is more lenient with what Strings you can pass to this method.

Yes, it is valid (at compile time). And it is already in this format.
This though doesn't exclude the option that you have some issues at runtime.

So if any of MM and dd is 00, it throws this exception.
The exception message is kind of misleading IMHO
(I mean it would have been nicer if it was saying:
'invalid month' / 'invalid day of month' at runtime).

Note that if you specify this:

Timestamp ts = Timestamp.valueOf("0000-01-01 00:00:00.000000");

the parsed ts value is actually

0001-01-01 00:00:00.000000

So to avoid surprises like this, I would make sure
I specify some valid yyyy mm and dd values as
it seems Timestamp is doing some conversions ,
internally when not all values (yyyy, mm, dd) are valid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top