Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subtract two calendar object in Android

Say for example I have two calendar object with the format of "MM/dd/yyyy". How do I subtract them and should return the number of days? For example, "9/7/2014" - "9/1/2014" and returns 7.

Calendar calendar = Calendar.getInstance();
Calendar today = Calendar.getInstance();
like image 294
kmhigashioka Avatar asked Dec 15 '22 20:12

kmhigashioka


1 Answers

You may try like this:

Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.DAY_OF_MONTH,7);
  calendar.set(Calendar.MONTH,8); 
  calendar.set(Calendar.YEAR, 2014);

  Calendar today = Calendar.getInstance();

  long difference = today.getTimeInMillis() - calendar.getTimeInMillis();
  int days = (int) (difference/ (1000*60*60*24));
like image 188
Rahul Tripathi Avatar answered Jan 05 '23 06:01

Rahul Tripathi