Reputation: 33
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?
Upvotes: 3
Views: 481
Reputation: 339332
Duration
.between(
Instant.now() ,
myJavaUtilDate.toInstant()
)
.toMillis()
You are using terrible classes that were years ago supplanted by the modern java.time classes defined in JSR 310. Never use Calendar
, Date
, SimpleDateFormat
.
Instant
If you were handed a java.util.Date
object, immediately convert to a Instant
object. Use new conversion methods added to the old classes.
Instant then = myJavaUtilDate.toInstant() ;
An Instant
represents a moment as seen “in UTC”, with an offset of zero hours-minutes-seconds.
Capture the current moment as seen in UTC.
Instant now = Instant.now() ;
Calculate time to elapse.
Duration d = Duration.between( now , then ) ;
Verify that your target moment is indeed in the future.
if( d.isNegative() ) { … deal with faulty input … }
Generally best to pass around a Duration
object rather than a mere integer counting milliseconds or nanoseconds. But if need be, you can extract a count from the Duration
object.
long milliseconds = d.toMillis() ;
Notice that by working in UTC, we need not deal with any time zone issues.
If using Android 26+, this functionality is built-in. For earlier Android, the latest tooling brings most of this functionality via “API de-sugaring”.
Upvotes: 1