public class MainActivity extends FragmentActivity {
    static EditText metTodate, metFromdate, metInTime, metOutTime;
    static long no_of_days1;
    static long no_of_days2;

    static TextView no_of_days;
    static TextView no_of_days3;
    public static String str;

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

        metTodate = (EditText)findViewById(R.id.etTodate);
        metFromdate = (EditText)findViewById(R.id.etFromdate);
        metInTime = (EditText)findViewById(R.id.etInTime);
        metOutTime = (EditText)findViewById(R.id.etOutTime);

        no_of_days = (TextView)findViewById(R.id.etnoofdays);


        // Here is my method where I want my text view to display dates.
        no_of_days.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calendarDaysBetween(Calendar metFromdate, Calendar metTodate);
            }
        });
    }

    // Both date picker dialogs
    public void showTruitonDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

    public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            // Create a new instance of DatePickerDialog and return it

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
            metTodate.setText(day + "/" + (month + 1) + "/" + year);
        }
    }

    public void showFromDatePickerDialog(View v) {
        DialogFragment newFragment = new FromDatePickerFragment();
        newFragment.show(getSupportFragmentManager(), "datePicker");
    }

    public static class FromDatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c1 = Calendar.getInstance();
            int year = c1.get(Calendar.YEAR);
            int month = c1.get(Calendar.MONTH);
            int day1 = c1.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day1);
            // calendarDaysBetween(Calendar metFromdate, Calendar metTodate);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            // Do something with the date chosen by the user
            metFromdate.setText(day + "/" + (month + 1) + "/" + year);
        }
    }

    public static long calendarDaysBetween(Calendar metFromdate,
Calendar metTodate) {
        // Create copies so we don't update the original calendars.

        Calendar start = Calendar.getInstance();
        start.setTimeZone(metFromdate.getTimeZone());
        start.setTimeInMillis(metFromdate.getTimeInMillis());

        Calendar end = Calendar.getInstance();
        end.setTimeZone(metTodate.getTimeZone());
        end.setTimeInMillis(metTodate.getTimeInMillis());

        // Set the copies to be at midnight, but keep the day information.

        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);

        end.set(Calendar.HOUR_OF_DAY, 0);
        end.set(Calendar.MINUTE, 0);
        end.set(Calendar.SECOND, 0);
        end.set(Calendar.MILLISECOND, 0);

        // At this point, each calendar is set to midnight on
        // their respective days. Now use TimeUnit.MILLISECONDS to
        // compute the number of full days between the two of them.
        no_of_days1 =
TimeUnit.MILLISECONDS.toDays(Math.abs(end.getTimeInMillis() -
start.getTimeInMillis()));

        String finalresult = new Double(no_of_days1).toString();
        no_of_days.setText(finalresult);

        return no_of_days1;
    }}

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/CAA5attsVegpfgc76sOr8m%3D3zSL2jhZd6_euPkk7gEsY5s-aN8A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to