JavaFAQ Home » Text processing
Tip: What is important to remember when
you use SimpleDateFormat...
When parsing a date string using the abbreviated year pattern ("y" or "yy"),
SimpleDateFormat must interpret the abbreviated year relative to some century.
It does this by adjusting dates to be within 80 years before and 20 years after
the time the SimpleDateFormat instance is created.
For example, using a pattern of "MM/dd/yy" and a SimpleDateFormat instance
created on Jan 1, 1997, the string "01/11/12" would be interpreted as Jan 11,
2012 while the string "05/04/64" would be interpreted as May 4, 1964.
During parsing, only strings consisting of exactly two digits, as defined by
Character.isDigit(char), will be parsed into the default century. Any other
numeric string, such as a one digit string, a three or more digit string, or a
two digit string that isn't all digits (for example, "-1"), is interpreted
literally. So "01/02/3" or "01/02/003" are parsed, using the same pattern, as
Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
If the year pattern has more than two 'y' characters, the year is interpreted
literally, regardless of the number of digits.
So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
For time zones that have no names, use strings GMT+hours:minutes or
The calendar defines what is the first day of the week, the first week of the
year, whether hours are zero based or not (0 vs 12 or24), and the time zone.
There is one common decimal format to handle all the numbers; the digit count is
handled programmatically according to the pattern.
Our older tips: March 22, 2001 - October 21, 2002
All published and not published on site tips you can find
Printer Friendly Page
Send to a Friend
Search here again if you need more info!