Re: DateFormat question



On Thu, 31 May 2007 20:32:58 +0100, Tom Hawtin <usenet@xxxxxxxxxxxxxxxxx> wrote:

Daniel Dyer wrote:
just be that you get an instance of SimpleDateFormat, which is a public class that you can instantiate directly. I don't know exactly which type is returned, but the idea is that you don't need to know. If you are curious, you can call getClass() on the object returned and print out its type.

It's not entirely true that you don't need to know. If you want to do some constrained input you need to more information than DateFormat, so casting to SimpleDateFormat would be useful. Also if you serialise it, you need to know whether you can deserialise it, and whether you are likely to be able to deserialise it on other machines.

Fair point, but *usually* it's enough just to know that you're getting a DateFormat of some kind and in these cases you don't care what the concrete type is.

In general, if you do need to cast to a more specific type than a method declares to return, it raises questions about the design of the API or the way you are using it. It's not necessarily wrong, you can't do much else with interfaces like Serializable or java.util.RandomAccess. However, in the case of casting the result of DateFormate.getDateInstance (a poorly named method IMO) to SimpleDateFormat, perhaps you should just use SimpleDateFormat directly if you need to be certain about the type?

Dan.

--
Daniel Dyer
http//www.uncommons.org
.