Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Calendar returns same millisecond value for two different dates

Tags:

java

calendar

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

Code is:

  public int getTotalStartEndTime( java.sql.Date startdate, java.sql.Date enddate, java.sql.Time starttime, java.sql.Time endtime )

{

if(startdate != null & enddate != null && starttime !=null && 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;

Any help would be greatly appreciated, as I cannot work out what is happening!

like image 603
James Avatar asked May 31 '26 18:05

James


2 Answers

calendar month is 0-11, in your code, you parsing date from string and month 10 is converted to november, which has not 31 days and set to first december.

like image 129
karci10 Avatar answered Jun 03 '26 07:06

karci10


As it has been said, MONTH is 0-11. Your code didn't throw an exception since the default value of lenient is true. You should set it to false (unless you explicitly want this behavior) to detect this kind of situation more easily :

calendar1.setLenient(false);
calendar2.setLenient(false);
like image 25
Inouz Avatar answered Jun 03 '26 08:06

Inouz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!