Reputation: 23
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!
Upvotes: 2
Views: 2304
Reputation: 34323
It's not actually answering your question, but if all you need is the value of diff
, there is no need to work with Calendar
instances, but you can replace your code entirely with:
long diff = enddate.getTime() - startdate.getTime();
If you actually need the Calendar
objects for other operations, there is still no need to parse the string representation of the dates, you can simply set the Calendar
to the Date
value with one operation:
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(startdate.getTime());
Upvotes: -1
Reputation: 76
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);
Upvotes: 1
Reputation: 375
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.
Upvotes: 10