如何在Android Studio中选定的日期添加天数
我在 DatePicker 中选择的日期(不是当前日期或今天日期)是 date1 : SelectedDate.setText(date1) = "05-04-2022"。
首先,我想向 date1 添加 5 天以获取 date2 并将其显示在 EditText2 中以获取:InputDate.setText(date2) = "10-04-2022"。
其次向 date1 添加 13 天以获取 date3 并将其显示在 EditText3 中以获取: 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();
}
});
My selected date in the DatePicker (not the current Date or Today Date) is date1 : SelectedDate.setText(date1) = "05-04-2022".
First i want to add 5 days to date1 to get date2 and display it in EditText2 to get: InputDate.setText(date2) = "10-04-2022".
Second add 13 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();
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案,如果还有其他建议,我很高兴:
I found the solution and if there is another proposal it is with pleasure: