java - SimpleDateFormat problems with 2 year date -
i'm trying understand 2 things:
- why doesn't following code throw exception (since
simpledateformatnot lenient) - it doesn't throw exception, why parsing year 0013 (instead of using rules here +80:-20 years today rule)
here's code
import java.text.simpledateformat; import java.util.calendar; import java.util.date; public class testdate { public static void main(string[] args) throws exception { simpledateformat format = new simpledateformat("dd/mm/yyyy"); format.setlenient(false); date date = format.parse("01/01/13"); // since has 2 digit year, expect exception thrown system.out.println(date); // prints sun jan 01 00:00:00 gmt 13 calendar cal = calendar.getinstance(); cal.settime(date); system.out.println(cal.get(calendar.year)); // prints 13 } } if makes difference, i'm using java 1.6.0_38-b05 on ubuntu
simpledateformat api:
for parsing, if number of pattern letters more 2, year interpreted literally, regardless of number of digits. using pattern "mm/dd/yyyy", "01/11/12" parses jan 11, 12 a.d.
as lenient, when it's set false parse throws exception invalid dates, eg 01/32/12, while in lenient mode date treated 02/01/12. simpledateformat uses calendar internally, details leniency can found in calendar api.
Comments
Post a Comment