I m writing a program which involves converting java.util.Date to java.sql.Date...
I've done it using getTime() method...
java.util.Date dt = new java.util.Date();
java.text.DateFormat df = new java.text.SimpleDateFormat("dd/MM/yyyy");
dt=df.parse("06/12/0785");
java.sql.Date sqldate = new java.sql.Date(dt.getTime());
System.out.print("\n\n\n "+dt.getTime()+" "+sqldate);
I m getting a negative value for dt.getTime()
and the value is correct for sqldate
...
The doubt i've is
`Is it safe to use negative seconds as epoch or is it vulnerable to bugs while implementing it in database in any way.....???
AND
It's printing wrong date for sqldate on setting date as 00/00/0000
instead of the one mentioned in the example....what might be the cause......and does there lie a solution.......???
If you parse a "00" as month, it will be regarded as December. The day "00" is the last day of the previous month, so the combinations is parsed as the 30th of November. As for the zero year, see Year 0000 in java. Negative values for .getTime() are no problem at all.
Well, in epoch time, "0" would be the equivalent to January 1, 1970. Any date earlier than that would come out negative when you try to parse it.
Try changing the date string you are parsing to any date after 1970 and you will get a proper value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With