Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using java epoch seconds in negative

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.......???

like image 619
Arjun K P Avatar asked Sep 15 '25 00:09

Arjun K P


2 Answers

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.

like image 172
Arne Avatar answered Sep 17 '25 13:09

Arne


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.

like image 30
namenamename Avatar answered Sep 17 '25 13:09

namenamename