Reputation: 11
I have a validation method which will take two input as start date and end date and check whether end date is valid i.e. after start date.
I used Calendar method getTimeInMillis()
to get milliseconds of two dates and get the difference. If the difference is negative then its invalid end date i.e. end date is before start date.
The millisecond value of end date should be always greater that start date. But in some cases getTimeInMillis()
giving greater value in case of start date.
Here is my code:
public class ValidationProcessor {
private void testTimeDiff(String startDate, String endDate) throws Exception {
Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
// Set the dates for calendars
cal1.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]),
Integer.parseInt(startDate.split("-")[2]));
cal2.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]),
Integer.parseInt(endDate.split("-")[2]));
long milis1 = cal1.getTimeInMillis();
long milis2 = cal2.getTimeInMillis();
System.out.println("milis1::: " + milis1);
System.out.println("milis2::: " + milis2);
long diff = milis2 - milis1;
System.out.println("diff::: " + diff);
if (diff <= 0) {
throw new Exception("Invalid End Date. End Date Must Be After Start Date");
}
}
public static void main(String args[]) {
ValidationProcessor test = new ValidationProcessor();
try {
test.testTimeDiff("2015-01-31", "2015-02-01");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The output is:
milis1::: 1425367717308
milis2::: 1425194917308
diff::: -172800000
java.lang.Exception: Invalid End Date. End Date Must Be After Start Date
Here 2015-01-31 is start date and 2015-02-01 is end date. As start date(2015-01-31) is coming as greater value in milliseconds than end date(2015-02-01) so its giving wrong result.
So is this a problem with method getTimeInMillis()
of Calendar
class or I am doing something wrong?
Upvotes: 1
Views: 2068
Reputation: 11
One catch is that, if we set month with -1 then it gives correct result. like,
cal1.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]) -1,
Integer.parseInt(startDate.split("-")[2]));
cal2.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]-1),
Integer.parseInt(endDate.split("-")[2]));
it gives, diff as 86400000
Upvotes: 0
Reputation: 6016
Use cal1.getTime().getAfter(cal2.getTime());
Update 1
To fill up your calendars you should do it like below
//cal1.set(Integer.parseInt(startDate.split("-")[0]), Integer.parseInt(startDate.split("-")[1]), Integer.parseInt(startDate.split("-")[2]));
//cal2.set(Integer.parseInt(endDate.split("-")[0]), Integer.parseInt(endDate.split("-")[1]), Integer.parseInt(endDate.split("-")[2]));
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cal1.setTime(df.parse(startDate));
cal2.setTime(df.parse(endDate));
Then your original code will work too as well.
Update 2
Whereas you can avoid using Calendar for this particular use and use the date objects directly.
Date date1 = df.parse(startDate);
Date date2 = df.parse(endDate);
long milis1 = date1.getTime();
long milis2 = date2.getTime();
Upvotes: 0
Reputation: 81
It could be caused by the fact that Calendar.getInstance()
gives you the current timestamp (with all fields like hours, minutes, etc. filled in) and you don't explicitely clear those extra fields (you only set the year, month and date).
Also, keep in mind dat Calendar.set(int year, int month, int day)
uses a 0-based month (e.g. Jan = 0, Feb = 1, etc.), so your conversion is currently wrong.
Update: I think it's the combination of both; your test date 2015-01-31 is parsed as February 31, 2015, which is of course invalid, resulting in an unexpected getTimeInMillis()
value.
Upvotes: 1