Display
Displaying dates in an application also depends to a great degree on context. Although you should always use DateFormat or its equivivalent to get locally formatted dates and times, the exact format you get can and should be tailored to the application's needs.
In particular, you may want to use a locale-neutral, unambiguous format for values such as timestamps.
Date and times also have an orthogonal component which can be quite irksome: timezones. The timezone of the server may not be the most approprite one for display of dates. GMT might not be a good choice either. In any case you need to ensure that display and parsing of dates and times is handled appropriately.
Information on timezones can be found in Timezones
Here are some of the formats:
- DateFormat.getInstance():
February 8, 2010 2:37:27 PM PST
January 1, 2000 12:01:00 AM PST - French, Swiss, Japanese, Hindi and Albanian examples:
-
8 février 2010 14:37:27 PST
1 janvier 2000 00:01:00 PST -
8. Februar 2010 14:37:27 PST
1. Januar 2000 00:01:00 PST -
2010/02/08 14:37:27 PST
2000/01/01 0:01:00 PST -
८ फ़रवरी, २०१० २:३७:२७ अपराह्न PST
१ जनवरी, २००० १२:०१:०० पूर्वाह्न PST -
2010-02-08 2.37.27.MD PST
2000-01-01 12.01.00.PD PST
-
8 février 2010 14:37:27 PST
- Alternate formats
- IME (keyboarding) problems, such as wide punctuation
As you can see, date formats vary very widely.
Use at least two locales with divergent formats as a baseline.
Hindi demonstrates numeric shaping in action. There are no less than 22 sets of numbers in Unicode, most of which are not Latin (1, 2, 3) digits. Java can parse these successfully in most cases.
Input
Input of dates and times is very difficult to do correctly. The basic DateFormat.parse method will suffice in some cases, but you should be aware of common input mistakes and other issues inherent in relying on DateFormat.
There are a variety of other things that can confuse DateFormat. These include:
It is possible to construct a more aggressive parser, such as com.wm.g11n.text.DateUtils, that handles these cases. The form below feeds both the result Java DateFormat and the DateUtils classes. Compare the results each gets.