Enhancement: DateTimeFormat: Add format/formatYear parameter to guess century on yyyy

90 views
Skip to first unread message

HerrB92

unread,
Dec 1, 2010, 8:46:03 AM12/1/10
to Google Web Toolkit Contributors
Hi,

I'm sorry, I don't know if there is and this is the right place to
place an enhancement wish. So, thank you for reading... ;-)

If a parsed date contains only a 2-digit year, then the century will
be guessed based on the current century +/- 20 years (in short). If
you format a date, no guessing takes place.

In the current century, people tend to go back to 2-digit typing, as
the "20" in "2010" shouldn't change in the next 90 years.

The latest fix ensures at least, that formatting a date results in the
specified digit count. But "10" will become "0010" with a "yyyy"
pattern. This is the documented result - but may not be expected by
the users.

Wish:
Add a parameter to formatYear() (and thus to format()) to guess the
century, if a 2-digit year and - to keep it simple - a 4-digit-year
pattern is provided.

E.g. format(<1.1.10>,<guessCentury=true>) -> 01.01.2010, if
datePattern = "dd.MM.yyyy".

Thank you.

Regards,
HerrB

John Tamplin

unread,
Dec 1, 2010, 11:17:06 AM12/1/10
to google-web-tool...@googlegroups.com
I don't think you ever want a Date to have the two-digit year -- things like which day of the week a date is, leap year or not, etc will all be wrong.  So, any guessing should only be at the point the date comes into the system, whether from the user, a database, or whatever.  Then when you format it for printing, you can choose to use two-digit years if you prefer.

--
John A. Tamplin
Software Engineer (GWT), Google

HerrB92

unread,
Dec 1, 2010, 11:50:01 AM12/1/10
to Google Web Toolkit Contributors
Err, yes ... I don't ever want a 2-digit year, also.

I have people, that enter "10" and expect to get "2010". Currently,
you will get "0010" (with a "yyyy" pattern).

But I think, I get the point: If there is textfield and a user enters
1.1.10 and default date parsing is used (pattern "dd.MM.yyyy"), you
will get a valid date object - 10 years AC. My request would mean,
that if .format() is applied to that date, that the century is
replaced by the guessed century, if the given century equals "00".

As this would be nice for me, it would be weired for others. Ok, never
mind...

Björn Behrens
b!tech berlin

John Tamplin

unread,
Dec 1, 2010, 11:53:33 AM12/1/10
to google-web-tool...@googlegroups.com
On Wed, Dec 1, 2010 at 11:50 AM, HerrB92 <her...@googlemail.com> wrote:
Err, yes ... I don't ever want a 2-digit year, also.

I have people, that enter "10" and expect to get "2010". Currently,
you will get "0010" (with a "yyyy" pattern).

But I think, I get the point: If there is textfield and a user enters
1.1.10 and default date parsing is used (pattern "dd.MM.yyyy"), you
will get a valid date object - 10 years AC. My request would mean,
that if .format() is applied to that date, that the century is
replaced by the guessed century, if the given century equals "00".

My point is that you want to fix that at the point the date is entered, not to just display the date differently, since many other things will depend on the year being correct.

Ie, Date d = fixMissingCentury(dtf.parse(dateField));

where fixMissingCentury detects a first-century year and changes it according to your needs.

jhulford

unread,
Dec 2, 2010, 9:50:50 AM12/2/10
to Google Web Toolkit Contributors
HerrB, if you're using a DateBox you can pretty easily get the
behavior you're after by providing your own implementation of the
DateBox.Format interface to the DateBox constructor. Your
implementation can pretty much just be a simple wrapper around the
Default one that adjusts the century to be 2000 when it's something
like 10. It might be nice if the DefaultFormat had a setting like
(http://download.oracle.com/javase/7/docs/api/java/text/
SimpleDateFormat.html#set2DigitYearStart%28java.util.Date%29), but
it's pretty easy to work around the lack of that method.

On Dec 1, 11:53 am, John Tamplin <j...@google.com> wrote:

Cy Kilbourn

unread,
Mar 24, 2018, 6:46:04 PM3/24/18
to GWT Contributors
If you use the "yy" pattern instead, the default behavior will be to guess the year based on the 20 yrs forward, 80 yrs back logic.
Reply all
Reply to author
Forward
0 new messages