i have writen a Method to calculate the difference between two Dates.
here is the Method:
/**
* Method to calculate time Difference between to Times Strings
*
* @param time1 The Current Time Time
* @param time2 Item Start Time
* @param item the Item should to be played
* @return Difference between Two Times as Seconds
*/
public static long timeDifference(String time1, String time2, Item item) {
// get the current Calendar Instance
Calendar now = Calendar.getInstance();
// calculate the days Difference between the Item Start Day and the current Day
int daysToAdd=0;
if(RestOperations.dayoffWeek<item.getDay()){
daysToAdd= (RestOperations.dayoffWeek-item.getDay()) * (-1);
}
else{
daysToAdd = 7-(RestOperations.dayoffWeek-item.getDay());
}
// parse the passed Times Strings to Date Objects
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = null;
Date date2 = null;
try {
date1 = format.parse(time1);
date2 = format.parse(time2);
} catch (ParseException e) {
e.printStackTrace();
}
// set the Current Time to the current Calendar instance
now.setTime(date1);
// create Date for the Item
Calendar itemDate = Calendar.getInstance();
// set the Item Start Time to the Item Calendar Instance
itemDate.setTime(date2);
// add the Days Difference to the Item Calendar Insatnce to determinate the Start Date of the Item
itemDate.add(Calendar.DAY_OF_MONTH, daysToAdd);
// get Difference between the both Calendar Instances to determinate how much Seconds must the Thread be stopped
long difference = itemDate.getTimeInMillis()-now.getTimeInMillis();
/* if (date1 != null & date2 != null) {
difference = (int) (date2.getTime() - date1.getTime());
}*/
// start for debugging!
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int sec = now.get(Calendar.SECOND);
int hour2 = itemDate.get(Calendar.HOUR_OF_DAY);
int minute2 = itemDate.get(Calendar.MINUTE);
int sec2 = itemDate.get(Calendar.SECOND);
String currDate = now.get(Calendar.YEAR)+"-"+now.get(Calendar.MONTH)+"-"+now.get(Calendar.DAY_OF_MONTH)+" "+hour + ":" + minute + ":" + sec;
String itDate = itemDate.get(Calendar.YEAR)+"-"+itemDate.get(Calendar.MONTH)+"-"+itemDate.get(Calendar.DAY_OF_MONTH)+" "+hour2 + ":" + minute2 + ":" + sec2;
Log.i("CurDate: ", currDate);
Log.i("ItemStartDate: ", itDate);
Log.i("difff: ", difference+"");
// end of debugging
return difference;
}
When i run this Method for this example:
duration = Utility.timeDifference("11:8:56","07:04:43",6);
i got the year = 1970, The Month = 0 and the Duration is negative (<0)
So for explanation the first Parameter corresponds to the current Time. The second Parameter corresponds when the Item should start to Play and the third Parameter corresponds the Day of Week when the Item should start to Play.
At the above code i try to do the following:
The first Parameter should be transformed to the current Date: 20-11-2015 11:08:56.
The Second Parameter should be transformed using the third Parameter to the current Date: 21-11-2015 07:04:43.
can someone tell me please where could be the Error?