如何在android studio中为所选日期添加天数



我在DatePicker中选择的日期(不是当前日期或今天日期(是date1:SelectedDate.setText(date1(="2022年4月5日">
首先,我想将5天添加到date1以获得date2,并将其显示在EditText2中以获得:InputDate.setText(date2(="2022年4月10日">
Second add13 days to date1 to get date3 and display it in EditText3 to get:tv_editDate.setText(date3(="18-04-2022";

SelectedDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
String myFormat = "dd/MM/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
sdf = new SimpleDateFormat("dd/MM/yyyy");
DatePickerDialog datePicker = new DatePickerDialog(Activity_races.this, new 
DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int 
dayOfMonth) {
String date1 = String.valueOf(dayOfMonth) + "/" + 
String.valueOf(monthOfYear+1) + "/" + String.valueOf(year);

SelectDate.setText(date1);                  
// Todoo .. add 5 days to date1
inputDate.setText(Date2);
// Todoo .. add 13 days to date1
tv_editDate.setText(date3);
}
}, yy, mm, dd);
datePicker.show();
}
});

我找到了解决方案,如果还有其他建议,我很高兴:

inputLabel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
String myFormat = "dd/MM/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
sdf = new SimpleDateFormat("dd/MM/yyyy");
DatePickerDialog datePicker = new DatePickerDialog(Activity_races.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date1 = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear+1) + "/" + String.valueOf(year);
// Todoo
//Given Date in String format
String oldDate = date1;
//Specifying date format that matches the given date
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
Calendar c1 = Calendar.getInstance();
try{
//Setting the date to the given date
c.setTime(sdf.parse(oldDate));
c1.setTime(sdf.parse(oldDate));
}catch(ParseException e){
e.printStackTrace();
}
//Number of Days to add
c.add(Calendar.DAY_OF_MONTH, 5);
c1.add(Calendar.DAY_OF_MONTH, 13);
//Date after adding the days to the given date
String Date2 = sdf.format(c.getTime());
String Date3 = sdf.format(c1.getTime());
//Displaying the new Date after addition of Days
SelectDate.setText(date1);
inputDate.setText(Date2);
tv_Date.setText(Date3);
}
}, yy, mm, dd);
datePicker.show();
}
});

相关内容

  • 没有找到相关文章