I need the difference between current time and specific time in future so that i can pass countdown timer as a parameter. I have function for this operation but it calculates wrong. Here is my function that calculates difference
public Date RetriveLeftTime(Date incomingTime) {
DateFormat milisFormat = new SimpleDateFormat("HH:mm:ss");
Date moment = Calendar.getInstance().getTime();
milisFormat.format(moment);
Date configuredTime = ConfigureTime(incomingTime);
milisFormat.format(configuredTime);
long leftTime =configuredTime.getTime()-moment.getTime();
Date result = new Date(leftTime);
result = ConfigureTime(result);
Log.d("Result",result.toString());
return result;
}
The ConfigureTime() function for configuring date details
public Date ConfigureTime(Date date){
Calendar today = Calendar.getInstance();
int date = today.get(Calendar.DATE);
int year = today.get(Calendar.YEAR);
int day = today.get(Calendar.DAY_OF_WEEK);
int month = today.get(Calendar.MONTH);
int zone = today.get(Calendar.ZONE_OFFSET);
Calendar time = Calendar.getInstance();
time.setTime(date);
time.set(Calendar.YEAR, year);
time.set(Calendar.MONTH, month);
time.set(Calendar.DAY_OF_WEEK, day);
time.set(Calendar.DATE, date);
time.set(Calendar.ZONE_OFFSET,zone);
Log.d("ConfigureTime()",time.getTime().toString());
Log.d("Current Time",today.getTime().toString());
return time.getTime();
}
I have looked similar post about this issue.I checked and configured all my time specification like timezone,date,year but still get wrong result.I don't get why it is happening.Maybe my code is doing it wrong way i don't know. What do i do wrong? Most people recommend to use joda time but i took it little too far to switch joda time.Is there a solution before switching to joda time?