0

I'm new to android and trying to develop a simple application in which the user will enter two dates and by clicking a button he will receive the difference between them in days. Here is my code, unfortunateקly the output is always 0.

package il.ac.hit.datetodate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText ed1 = (EditText) findViewById(R.id.editText1);
        final EditText ed2 = (EditText) findViewById(R.id.editText2);
        final TextView tv = (TextView) findViewById(R.id.textView2);
        final Button bt = (Button) findViewById(R.id.button1);

        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Calendar cal1 = Calendar.getInstance();
                Calendar cal2 = Calendar.getInstance();
                SimpleDateFormat sdf1 = new SimpleDateFormat("MMM dd yyyy");
                SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd yyyy");

                try {
                    cal1.setTime(sdf1.parse(ed1.getText().toString()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                try {
                    cal2.setTime(sdf2.parse(ed2.getText().toString()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                long days1 = cal1.getTimeInMillis();
                long days2 = cal2.getTimeInMillis();
                long diff = (days2 - days1)/(1000*60*60*24);
                tv.setText(String.valueOf(diff));
            }
        });
    }
}
Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
  • First of all one SimpleDateFormat object is enough to do both the operation. – SKT Sep 06 '13 at 12:04
  • http://stackoverflow.com/questions/3838527/android-java-date-difference-in-days... check out this link, it may help you... – ADT Sep 06 '13 at 12:05

3 Answers3

0

With the SimpleDateFormat you can parse a String to a Calendar-Object:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
JavaDM
  • 851
  • 1
  • 6
  • 29
0

Use below code.

 Calendar thatDay = Calendar.getInstance();
 thatDay.set(Calendar.DAY_OF_MONTH,25);
 thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
 thatDay.set(Calendar.YEAR, 1985);

 Calendar today = Calendar.getInstance();

 long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

An approximation...

long days = diff / (24 * 60 * 60 * 1000);

To Parse the date from a string, you could use

String strThatDay = "1985/08/25";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date d = null;
try {
   d = formatter.parse(strThatDay);//catch exception
} catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} 
Calendar thatDay = Calendar.getInstance();
thatDay.setTime(d); //rest is the same....

You Could also do Integer.parseInt() on it's Substrings to obtain their numeric values.

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
0

See if this works

If you want the difference between two times you can use this ::

Date date1 = cal.getTime(); String endTime = "2013/29/08 18:59:59"; 
SimpleDateFormat dateFormat =     new SimpleDateFormat( "yyyy/dd/MM HH:mm:ss"); 
Date dtEndTime = dateFormat.parse(endTime); 
DecimalFormat formatter = new DecimalFormat("###,###"); 
long diff = dtEndTime.getTime() - date1.getTime();

the diff will give you the difference in millisecs.

Ritaban
  • 763
  • 4
  • 8