Niks Niks - 16 days ago 12
Android Question

Time difference between two times

I want to display the difference between two times in hh:mm format.

The first time is from a database and the second time is the system time. Time difference is updated every second.

How can I do that?

Currently I'm using two manual time if this works perfectly then I implement it into my apps.

public class MainActivity extends Activity
{
TextView mytext;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask()
{
public void run()
{
try
{
TextView txtCurrentTime= (TextView)findViewById(R.id.mytext);
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
Date Date1 = format.parse("08:00:12 pm");
Date Date2 = format.parse("05:30:12 pm");
long mills = Date1.getTime() - Date2.getTime();
Log.v("Data1", ""+Date1.getTime());
Log.v("Data2", ""+Date2.getTime());
int Hours = (int) (mills/(1000 * 60 * 60));
int Mins = (int) (mills % (1000*60*60));

String diff = Hours + ":" + Mins; // updated value every1 second
txtCurrentTime.setText(diff);
}
catch (Exception e)
{
e.printStackTrace();
}
}

}, 0, 1000);
}
}

Answer

To Calculate the difference between two dates you could try something like:

long mills = Date1.getTime() - Date2.getTime();
int Hours = millis/(1000 * 60 * 60);
int Mins = millis % (1000*60*60);

String diff = Hours + ":" + Mins; 

To update the Time Difference every second you can make use of Timer.

Timer updateTimer = new Timer();
updateTimer.schedule(new TimerTask() {
    public void run() {
        try {
            long mills = Date1.getTime() - Date2.getTime();
                int Hours = millis/(1000 * 60 * 60);
                 int Mins = millis % (1000*60*60);

                 String diff = Hours + ":" + Mins; // updated value every1 second
            } catch (Exception e) {
            e.printStackTrace();
        }
    }

}, 0, 1000); // here 1000 means 1000 mills i.e. 1 second

Edit : Working Code :

public class MainActivity extends Activity {

    private TextView txtCurrentTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtCurrentTime= (TextView)findViewById(R.id.mytext);
        Timer updateTimer = new Timer();
        updateTimer.schedule(new TimerTask() 
        {
            public void run() 
            {
                try 
                {

                    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                    Date Date1 = format.parse("08:00:12 pm");
                    Date Date2 = format.parse("05:30:12 pm");
                    long mills = Date1.getTime() - Date2.getTime();
                    Log.v("Data1", ""+Date1.getTime());
                    Log.v("Data2", ""+Date2.getTime());
                    int Hours = (int) (mills/(1000 * 60 * 60));
                    int Mins = (int) (mills/(1000*60)) % 60;

                    String diff = Hours + ":" + Mins; // updated value every1 second
                    txtCurrentTime.setText(diff);
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }

        }, 0, 1000);
    }
Comments