Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

android - 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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...