如何检查所选日期比android中的当前日期多8天

时间:2017-09-29 06:19:59

标签: android date

我正在开发一种功能,我希望选择比当前日期多8天的日期。我已经实施了一个案例但在某些情况下它不起作用。请帮忙

在某些情况下,我收到了错误的数据。

//相同的代码

//获取开始日期的代码

at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:220)
at org.openqa.selenium.remote.http.AbstractHttpCommandCodec.encode(AbstractHttpCommandCodec.java:118)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
at org.openqa.selenium.remote.RemoteExecuteMethod.execute(RemoteExecuteMethod.java:35)
at org.openqa.selenium.remote.RemoteMouse.mouseMove(RemoteMouse.java:89)
at org.openqa.selenium.support.events.internal.EventFiringMouse.mouseMove(EventFiringMouse.java:58)
at org.openqa.selenium.remote.server.handler.interactions.MouseMoveToLocation.call(MouseMoveToLocation.java:59)
at org.openqa.selenium.remote.server.handler.interactions.MouseMoveToLocation.call(MouseMoveToLocation.java:32)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:176)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

这是我实施的代码。

3 个答案:

答案 0 :(得分:1)

start_date.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    final Calendar c = Calendar.getInstance();
                    mYear = c.get(Calendar.YEAR);
                    mMonth = c.get(Calendar.MONTH);
                    mDay = c.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    int myear = year;
                                    int mmonth = monthOfYear + 1;
                                    int mday = dayOfMonth;

                                   String mDate = mday + "/" + mmonth + "/" + myear;

                                    if (show(mDate) > 8) {
                                       Toast.makeText(getActivity(), "Selected date is greater than 8 days", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(getActivity(), "Selected date is less than 8 days", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }, mYear, mMonth, mDay);
                    datePickerDialog.show();

                    break;
                case MotionEvent.ACTION_UP:
                    break;
            }
            return true;
        }
    });


private int show(String time) {
        try {
            String outputPattern = "dd/MM/yyyy";
            SimpleDateFormat format = new SimpleDateFormat(outputPattern);

            Date Date1 = format.parse(getdate());
            Date Date2 = format.parse(time);
            long mills = Date2.getTime() - Date1.getTime();
            long Day1 = mills / (1000 * 60 * 60);

            day = (int) Day1 / 24;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return day;
    }


    private String getdate() {
        String time = "";
        try {
            String outputPattern = "dd/MM/yyyy";

            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat(outputPattern);
            time = df.format(c.getTime());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return time;
    }

答案 1 :(得分:0)

long diffInMillisec =  currentdate.getTime()-registereddate.getTime();

        // Get difference between two dates in days
        long diffInDays = diffInMillisec / (24 * 60 * 60 * 1000);

if(diffInDays > 8){
    // Here your  code from when your current date is greater than 8 days..
}

答案 2 :(得分:0)

避免月份和年份检查:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
Date todayStart = cal.getTime();
cal.add(Calendar.DATE, 8);
Date plus8Days = cal.getTime();
cal.add(Calendar.DATE, 1);
Date plus9Days = cal.getTime();

然后只需检查所选日期是否在plus8Daysplus9Days之间。