In the following code I am converting a java.util.date into java.sql.date. I am confused at a point that while giving the date before epoch like given below and I have also used date.getTime() method which gives miliseconds from the epoch, it gave me the right time as shown in the results. My questions is why it didn't fail(as I assumed it should)? Why It gave the right time?
Code:
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); //defining format
String dateOfAdmissionInString = "22-01-1966 05:03:33";
Date utilDateOfAdmission = sdf2.parse(dateOfAdmissionInString);
java.sql.Timestamp sqlDateOfAdmission = new Java.sql.Timestamp(utilDateOfAdmission.getTime());
System.out.println("utilDateOfAdmission:" + utilDateOfAdmission);
System.out.println("sqlDateOfAdmission:" + sqlDateOfAdmission);
Result:
utilDateOfAdmission: Sat Jan 22 05:03:33 PKT 1966
sqlDateOfAdmission: 1966-01-22 05:03:33.0
My questions is why it didn't fail
Because the number of milliseconds since The Epoch is allowed to be negative, which indicates date/times before Jan 1st 1970 at midnight GMT.
Gratuitous example: Elvis' birthdate:
Date dt = new Date(-1103932800000L);
System.out.println(dt.toString());