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); } }
To calculate the minutes between two times, multiply the time difference by 1440, which is the number of minutes in one day (24 hours * 60 minutes = 1440).
To solve for time use the formula for time, t = d/s which means time equals distance divided by speed.
To Calculate the difference between two dates you could try something like:
long millis = date1.getTime() - date2.getTime(); int hours = (int) (millis / (1000 * 60 * 60)); int mins = (int) ((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 = (mills/(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); }
finally did it yuppiiieee ...
package com.timedynamicllyupdate; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { TextView current; private TextView txtCurrentTime; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Thread myThread = null; Runnable myRunnableThread = new CountDownRunner(); myThread= new Thread(myRunnableThread); myThread.start(); current= (TextView)findViewById(R.id.current); } public void doWork() { runOnUiThread(new Runnable() { public void run() { try { SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa"); txtCurrentTime= (TextView)findViewById(R.id.mytext); Date systemDate = Calendar.getInstance().getTime(); String myDate = sdf.format(systemDate); // txtCurrentTime.setText(myDate); Date Date1 = sdf.parse(myDate); Date Date2 = sdf.parse("02:50:00 pm"); long millse = Date1.getTime() - Date2.getTime(); long mills = Math.abs(millse); int Hours = (int) (mills/(1000 * 60 * 60)); int Mins = (int) (mills/(1000*60)) % 60; long Secs = (int) (mills / 1000) % 60; String diff = Hours + ":" + Mins + ":" + Secs; // updated value every1 second current.setText(diff); } catch (Exception e) { } } }); } class CountDownRunner implements Runnable { // @Override public void run() { while(!Thread.currentThread().isInterrupted()) { try { doWork(); Thread.sleep(1000); // Pause of 1 Second } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch(Exception e) { } } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
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