我正在创建日期选择器并遵循一些准则并在stackoverflow中回答。但显然,我的约会选择月份从零开始。
例如: 如果我选择2018年1月24日,它将输出为24/0/2018。
有人可以教我如何解决这个问题吗?并将日期格式更改为" mm-dd-yyyy"。三江源
etin = (EditText) findViewById(R.id.etcheckin);
etin.setInputType(InputType.TYPE_NULL);
etout = (EditText) findViewById(R.id.etcheckout);
etout.setInputType(InputType.TYPE_NULL);
etin.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
showTruitonDatePickerDialog(view);
}
});
etout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToDatePickerDialog(view);
}
});
}
public void showTruitonDatePickerDialog(View v){
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public void showToDatePickerDialog(View v){
DialogFragment newFragment = new ToDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public Dialog onCreateDialog(Bundle savedInstanceState){
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);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.set(2018,11 ,31);
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return datePickerDialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
etin.setText(day + "/" + month + "/" + year);
}
}
public static class ToDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
// Calendar startDateCalendar=Calendar.getInstance();
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
String getfromdate = etin.getText().toString().trim();
String getfrom[] = getfromdate.split("/");
int year,month,day;
year= Integer.parseInt(getfrom[2]);
month = Integer.parseInt(getfrom[1]);
day = Integer.parseInt(getfrom[0]);
final Calendar c = Calendar.getInstance();
c.set(year, month, day + 1);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.set(2018,11, 31);
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
return datePickerDialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, month, day, 0, 0, 0);
Date chosenDate = cal.getTime();
etout.setText(day + "/" + month + "/" + year);
}
}
答案 0 :(得分:0)
只需在每个月份中添加1并将结果存储到新变量中,然后将该变量用作所选月份。像这样:int mont = month+1;
。此外,我修改了代码的最后一个片段,以便我的解释付诸行动。
public void onDateSet(DatePicker view, int year, int month, int day) {
int mont = month+1;
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
cal.set(year, mont, day, 0, 0, 0);
Date chosenDate = cal.getTime();
etout.setText(day + "/" + mont + "/" + year);
}
mont
是新变量。
更新:要在您的签入中获得类似的结果,请在获取日期的代码中执行相同的操作。像这样:
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
int mont = month +1;
etin.setText(day + "/" + mont + "/" + year);
}
答案 1 :(得分:0)
您确实是在轮班工作。例如,对应于2018年10月31日,您将获得2018年9月31日。