android 选择多个日期,如何计算通过Android中的日期选择器选择的两个日期之间的天数...

我想计算从日期选择器中选择的日期之间的天数。我已经尽力了,但是找不到任何解决方案!请帮助我找到解决方案。这是当我们单击editText时显示日期选择器的代码。我到处搜索,但找不到任何合适的结果。

LeaveActivity.javapublic class LeaveActivity extends AppCompatActivity {

private static EditText edt_tofrom;

private static EditText edt_toDate;

private TextView no_ofDays;

private DatePickerDialog.OnDateSetListener edt_tofromlistener;

private DatePickerDialog.OnDateSetListener edt_toDatelistener;

// DatePickerDialogFragment mDatePickerDialogFragment;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_leave);

edt_tofrom = (EditText) findViewById(R.id.from_date);

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

// edt_toform.setText( sdf.format(Calendar.getTime()));

edt_toDate = (EditText) findViewById(R.id.to_date);

no_ofDays = (TextView) findViewById(R.id.edt_noOfDays);

// mDatePickerDialogFragment = new DatePickerDialogFragment();

// edt_toform.setOnClickListener(this);

// edt_toDate.setOnClickListener(this);

edt_tofrom.setOnClickListener(new View.OnClickListener() {

@RequiresApi(api = Build.VERSION_CODES.N)

@Override

public void onClick(View v) {

Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);

int month = calendar.get(Calendar.MONTH);

int day = calendar.get(Calendar.DAY_OF_MONTH);

int day1 = calendar.get(Calendar.DAY_OF_YEAR);

DatePickerDialog datePickerDialog = new DatePickerDialog(LeaveActivity.this,

edt_tofromlistener, year, month, day);

datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));

datePickerDialog.show();

}

});

edt_tofromlistener = new DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth){

month = month + 1;

Log.d("tag", "setDate: " + year + "/" + month + "/" + dayOfMonth + "");

String date1 = +year + "/" + month + "/" + dayOfMonth + "";

edt_tofrom.setText(date1);

}

};

edt_toDate.setOnClickListener(new View.OnClickListener() {

@RequiresApi(api = Build.VERSION_CODES.N)

@Override

public void onClick(View v) {

Calendar calendar = Calendar.getInstance();

int year = calendar.get(Calendar.YEAR);

int month = calendar.get(Calendar.MONTH);

int day = calendar.get(Calendar.DAY_OF_MONTH);

int day2 = calendar.get(Calendar.DAY_OF_YEAR);

DatePickerDialog datePickerDialog = new DatePickerDialog(LeaveActivity.this,

edt_toDatelistener, year, month, day);

datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));

datePickerDialog.show();

}

});

edt_toDatelistener = new DatePickerDialog.OnDateSetListener() {

@Override

public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

month = month + 1;

Log.d("tag", "setDate: " + year + "/" + month + "/" + dayOfMonth + "");

String date2 = +year + "/" + month + "/" + dayOfMonth + "";

edt_toDate.setText(date2);

}

};

}

}

activity_leave.xml<?xml version="1.0" encoding="utf-8"?>

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="10dp"

tools:context=".activities.LeaveActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="APPLY FOR LEAVE"

android:textSize="24sp"

android:layout_gravity="center"

android:textStyle="bold"

android:background="@drawable/editt_text_background"/>

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:background="@drawable/editt_text_background">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:orientation="horizontal"

android:layout_weight="1">

android:id="@+id/selectTxtView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="10dp"

android:layout_marginTop="10dp"

android:layout_marginEnd="10dp"

android:layout_marginBottom="10dp"

android:padding="10dp"

android:text="Select Leave Type"

android:textSize="15sp"

android:textStyle="bold"/>

android:id="@+id/spinner"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:layout_toEndOf="@id/selectTxtView"

android:background="@drawable/editt_text_background"

android:entries="@array/LeaveType"

android:padding="10dp" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_margin="5dp"

android:orientation="horizontal"

android:padding="10dp"

android:layout_weight="1">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="From Date:"

android:textColor="#000000"/>

android:id="@+id/from_date"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:ems="10"

android:layout_weight="1"

android:background="@drawable/editt_text_background"

android:clickable="true"

android:longClickable="false"

android:inputType="date"

android:layout_marginStart="36dp"

android:layout_marginEnd="20dp"

android:focusable="false"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:padding="10dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="5dp"

android:text="To Date:"

android:textColor="#000000"/>

android:id="@+id/to_date"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:ems="10"

android:background="@drawable/editt_text_background"

android:clickable="true"

android:longClickable="false"

android:inputType="date"

android:layout_marginStart="36dp"

android:layout_marginEnd="20dp"

android:focusable="false" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="10dp"

android:layout_weight="1">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="No.Of Days:"

android:textColor="#000"

android:padding="10dp"/>

android:id="@+id/edt_noOfDays"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@drawable/editt_text_background"/>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值