String representation of Date objects coming from date field
We have a form with a couple of date fields on (from and to) which populate an object called ClawbackPolicy, and which define a period over which a particular policy is valid. If I fetch strings representing these dates using Date.toString() on the date objects held in ClawbackPolicy I get a different format depending on whether the date field was typed into or selected using the date picker. If we use the date picker, Date.toString() returns something like this: 1990-01-01 00:00:00.0 But if we type the date into the field (using exactly the same format), Date.toString() returns this format: Tue Feb 02 00:00:00 GMT 2010 I solved the problems this gives me by forcing the format to a common mask using DateFormatter, but it seems strange that the string representations should differ just because we entered the data to the field a different way. They are the same form and the same field, after all. Can anyone comment?
In general, you should really avoid Date.toString, since it does not specify how the dates are formatted. Either use Date.getTime(), or if it needs to be human readable, a (Simple)DateFormat. toString results heavily depend on your locale settings, and the timezone of the provided date. So the DateFormatter is indeed the way to go.
From the official documentation:
public String toString()
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy.
zzz is the time zone (and may reflect daylight saving time). Standard time zone abbreviations include those recognized by the method parse. If time zone information is not available, then zzz is empty - that is, it consists of no characters at all.