I need to know if there is something like a timespan in android development?
in C# there is something like and I like to use it in two ways:
public long addSeconds(long dt,int sec) //method to add seconds in time
{
Date Dt = new Date(dt);
Calendar cal = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
sdf.setCalendar(cal);
cal.setTimeInMillis(Dt.getTime());
cal.add(Calendar.SECOND, sec);
return cal.getTime().getTime();
}
pass date and time in sec, it will return modified time...
Unfortunately, there's no TimeSpan
like class yet natively available in Java, but you can achieve this with few lines of code.
Calendar startDate = getStartDate();
Calendar endDate = getEndDate();
long totalMillis = endDate.getTimeInMillis() - startDate.getTimeInMillis();
int seconds = (int) (totalMillis / 1000) % 60;
int minutes = ((int)(totalMillis / 1000) / 60) % 60;
int hours = (int)(totalMillis / 1000) / 3600;
Android has DateUtils, the method "formatElapsedTime" does what you need if you give it the right input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With