Java 我希望我的文本视图在选择两个日期时显示天数的差异

Java 我希望我的文本视图在选择两个日期时显示天数的差异,java,android,android-calendar,android-datepicker,Java,Android,Android Calendar,Android Datepicker,我想在我的TextView中使用下面的calendarDaysBetween()方法,或者我想让我的TextView显示两个日期的差异。有人能帮我吗 public class MainActivity extends FragmentActivity { static EditText metTodate, metFromdate, metInTime, metOutTime; static long no_of_days1; static long no_of_days

我想在我的
TextView
中使用下面的
calendarDaysBetween()
方法,或者我想让我的
TextView
显示两个日期的差异。有人能帮我吗

public class MainActivity extends FragmentActivity {
    static EditText metTodate, metFromdate, metInTime, metOutTime;
    static long no_of_days1;
    static long no_of_days2;
    Button mbtnApplyLeave;
    ImageView mivBack;
    RadioButton halfday1, halfday2, first_half, second_half, first_half1, second_half1, full_day1, full_day2;
    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);
        mivBack = (ImageView)findViewById(R.id.ivBack);
        halfday1 = (RadioButton)findViewById(R.id.halfday1);
        halfday2 = (RadioButton)findViewById(R.id.halfday2);
        first_half = (RadioButton)findViewById(R.id.firsthalf1);
        second_half = (RadioButton)findViewById(R.id.secondhalf1);
        first_half1 = (RadioButton)findViewById(R.id.firsthalf2);
        second_half1 = (RadioButton)findViewById(R.id.secondhalf2);
        full_day1 = (RadioButton)findViewById(R.id.fullday1);
        full_day2 = (RadioButton)findViewById(R.id.fullday2);
        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;
    }
}
试试这个

SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date past = format.parse("05/06/2015");
Date now = new Date();

System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");

它将使您在分钟和小时上与当前日期有所不同,您可以使用自己的日期格式和日期对象

您可以使用以下代码获得天数上的日期差异

public void calendarDaysBetween(Calendar metFromdate, Calendar etTodate)
{
long diff = (metFromdate.getTimeInMillis() - metTodate.getTimeInMillis());
long no_of_days1 = Math.abs(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
 no_of_days.setText(no_of_days1+"");
}
第一个declear日历对象

public Calendar metFromdate= Calendar.getInstance();
public Calendar metTodate== Calendar.getInstance();
更改第一个日期选择器值

public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
       metFromdate.set(Calendar.YEAR, year);
    metFromdate.set(Calendar.MONTH, month);
    metFromdate.set(Calendar.DAY_OF_MONTH, day);
    }
public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        metTodate.set(Calendar.YEAR, year);
    metTodate.set(Calendar.MONTH, month);
    metTodate.set(Calendar.DAY_OF_MONTH, day);
    }
更改第二个日期选择器值

public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
       metFromdate.set(Calendar.YEAR, year);
    metFromdate.set(Calendar.MONTH, month);
    metFromdate.set(Calendar.DAY_OF_MONTH, day);
    }
public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        metTodate.set(Calendar.YEAR, year);
    metTodate.set(Calendar.MONTH, month);
    metTodate.set(Calendar.DAY_OF_MONTH, day);
    }
更改按钮单击

 no_of_days.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            calendarDaysBetween(metFromdate, metTodate);
        }
    });
你用过了

   Calendar date1 = Calendar.getInstance(); Calendar date2 =
   Calendar.getInstance();

   date1.clear();
   date1.set(datePicker1.getYear(),datePicker1.getMonth(),datePicker1.getDayOfMonth());
   date2.clear();
   date2.set(datePicker2.getYear(),datePicker2.getMonth(),datePicker2.getDayOfMonth());

   long diff = date2.getTimeInMillis() - date1.getTimeInMillis();

   float dayCount = (float) diff / (24 * 60 * 60 * 1000);

   textView.setText(Long.toString(diff) + " " + (int) dayCount);


试试乔达时间。对于android,我已经为不同的日期创建了一个两个日期选择器对话框方法。但问题是,我不能在另一个实例或方法中使用其他实例或方法。如果我想使用第三个方法,我已经计算了日期的差异。我不能在文本视图中单击no_of_days使用它,但我怎么能在代码中使用它呢我能得到这个。我也尝试过这个。我想让它在文本视图中显示它使用它no_of_days.setText(no_of_days1+);谢谢你的帮助。但我在所有情况下都得到了0。它并没有将日期相加使用它的long diff=(metFromdate.getTimeInMillis()-metTodate.getTimeInMillis());我仍然得到它0。你能告诉我什么是日期选择器在我的情况下???我是新的编程,这就是为什么我不能得到什么是日期选择器在这种情况下
 yourTextView.setText(getTimeAgo(long-your_past_date_millis));