Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Calendar returns same millisecond value for two different dates
    text
    copied!<p>This is my first question, so please be gentle with me! I am having a problem with some pre-existing java code. It is pretty simple, you pass it two dates in the format "2013-10-31", it then calculates the ms difference between the two values and then does some more calculations after that. The problem is that every now and again, even though two different dates are passed, they both have the same millisecond value. An example of this is if you pass "2013-10-31" and "2013-11-01", it returns the difference as 0. The ms values both being "1385856000000".</p> <p>Code is:</p> <pre><code> public int getTotalStartEndTime( java.sql.Date startdate, java.sql.Date enddate, java.sql.Time starttime, java.sql.Time endtime ) </code></pre> <p>{</p> <pre><code>if(startdate != null &amp; enddate != null &amp;&amp; starttime !=null &amp;&amp; endtime!= null){ Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); int styr = Integer.parseInt(startdate.toString().substring(0,startdate.toString().indexOf("-")),10); int stmm = Integer.parseInt(startdate.toString().substring(startdate.toString().indexOf("-")+1,startdate.toString().lastIndexOf("-")),10); int stdd = Integer.parseInt(startdate.toString().substring(startdate.toString().lastIndexOf("-")+1),10); int enyr = Integer.parseInt(enddate.toString().substring(0,enddate.toString().indexOf("-")),10); int enmm = Integer.parseInt(enddate.toString().substring(enddate.toString().indexOf("-")+1,enddate.toString().lastIndexOf("-")),10); int endd = Integer.parseInt(enddate.toString().substring(enddate.toString().lastIndexOf("-")+1),10); //calendar1.set(styr, stmm, stdd); calendar1.set(Calendar.YEAR, styr); calendar1.set(Calendar.MONTH, stmm); calendar1.set(Calendar.DAY_OF_MONTH, stdd); calendar1.set(Calendar.HOUR, 0); calendar1.set(Calendar.MINUTE, 0); calendar1.set(Calendar.SECOND, 0); calendar1.set(Calendar.MILLISECOND,0); //calendar2.set(enyr, enmm, endd); calendar2.set(Calendar.YEAR, enyr); calendar2.set(Calendar.MONTH, enmm); calendar2.set(Calendar.DAY_OF_MONTH, endd); calendar2.set(Calendar.HOUR, 0); calendar2.set(Calendar.MINUTE, 0); calendar2.set(Calendar.SECOND, 0); calendar2.set(Calendar.MILLISECOND,0); long milliseconds1 = calendar1.getTimeInMillis(); long milliseconds2 = calendar2.getTimeInMillis(); long diff = milliseconds2 - milliseconds1; </code></pre> <p>Any help would be greatly appreciated, as I cannot work out what is happening!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload