Java 如何显示第二天';从Android中的datepicker对话框中选择的日期开始选择日期?
目前,我正在页面加载时以单独的布局显示今天的日期和日期以及明天的日期和日期。但我的要求是 1) 无论我从Java 如何显示第二天';从Android中的datepicker对话框中选择的日期开始选择日期?,java,android,date,android-datepicker,android-date,Java,Android,Date,Android Datepicker,Android Date,目前,我正在页面加载时以单独的布局显示今天的日期和日期以及明天的日期和日期。但我的要求是 1) 无论我从日期选择器中选择的日期是什么,它都应该显示在第一个布局上 2) 第二个布局上应显示第二天的日期 我的第一个要求是工作正常,没有任何问题。(即),无论我选择哪一天,它都会显示在第一个布局上。没问题,但我的第二个要求有问题 考虑一下,如果我在日期选择器上选择2018年5月25日,它将按预期显示在第一个版面上,但不是显示第二天的日期为2018年5月26日,而是显示为2018年6月26日。但我需要它以
日期选择器中选择的日期是什么,
它都应该显示在第一个布局上
2) 第二个布局上应显示第二天的日期
我的第一个要求是工作正常,没有任何问题。(即),无论我选择哪一天,它都会显示在第一个布局上。没问题,但我的第二个要求有问题
考虑一下,如果我在日期选择器上选择2018年5月25日,
它将按预期显示在第一个版面上,但不是显示第二天的日期为2018年5月26日,而是显示为2018年6月26日。但我需要它以这样一种方式工作:如果我在日期选择器中选择2018年5月25日,
所选日期应显示在第一个版面上&下一个版面上应显示紧接第二天的日期(2018年5月26日)。这应该适用于我在日期选择器中选择的任何日期。
我哪里出错了
下面是我的Activity.Java:
public class PrivacyPolicyActivity extends AppCompatActivity {
LinearLayout layout1, layout2, layout3, todayLayout, tomorrowLayout;
ImageView calendarImgView;
TextView todayDate;
TextView todayDay;
TextView tommmorrowDate;
TextView tommorrowDay;
TextView todayLabel;
TextView tommorrowlabel;
DatePickerDialog datePickerDialog;
Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_privacy_policy);
calendar = Calendar.getInstance();
Date today = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date tomorrow = calendar.getTime();
final DateFormat dateFormat = new SimpleDateFormat("dd-MMM");
DateFormat day = new SimpleDateFormat("EEEE");
layout1 = (LinearLayout) findViewById(R.id.layout1);
layout2 = (LinearLayout) findViewById(R.id.layout2);
layout3 = (LinearLayout) findViewById(R.id.layout3);
todayDate = (TextView) findViewById(R.id.todayDate);
todayDate.setText(dateFormat.format(today));
todayDay = (TextView) findViewById(R.id.todayDay);
todayDay.setText(day.format(today));
tommmorrowDate = (TextView) findViewById(R.id.tommmorrowDate);
tommmorrowDate.setText(dateFormat.format(tomorrow));
tommorrowDay = (TextView) findViewById(R.id.tommorrowDay);
tommorrowDay.setText(day.format(tomorrow));
todayLayout = (LinearLayout) findViewById(R.id.todayLayout);
tomorrowLayout = (LinearLayout) findViewById(R.id.tomorrowLayout);
todayLabel = (TextView) findViewById(R.id.todayLabel);
tommorrowlabel = (TextView) findViewById(R.id.tommorrowlabel);
calendarImgView = (ImageView) findViewById(R.id.calendarImgView);
calendarImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(PrivacyPolicyActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
// SimpleDateFormat dateandmonth = new SimpleDateFormat("dd-MMM");
// Date date = new Date();
// String reqddate = dateandmonth.format(date);
//todayDate.setText(day + "/" + (month + 1) + "/" + year);
int cyear = datePicker.getYear();
int cmonth = datePicker.getMonth();
int cday = datePicker.getDayOfMonth();
Calendar cld = Calendar.getInstance();
cld.set(cyear, cmonth, cday);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
String strDate = format.format(cld.getTime());
todayDate.setText(strDate);
SimpleDateFormat dayfmt = new SimpleDateFormat("EEEE");
String dayselected = dayfmt.format(cld.getTime());
todayDay.setText(dayselected);
int nextYear = datePicker.getYear() + 1;
int nextMonth = datePicker.getMonth() + 1;
int nextDay = datePicker.getDayOfMonth() + 1;
Calendar nextcld = Calendar.getInstance();
nextcld.set(nextYear, nextMonth, nextDay);
SimpleDateFormat tom_format = new SimpleDateFormat("dd-MMM");
String tom_date = tom_format.format(nextcld.getTime());
tommmorrowDate.setText(tom_date);
SimpleDateFormat day_fmt = new SimpleDateFormat("EEEE");
String tom_day = day_fmt.format(cld.getTime());
tommorrowDay.setText(tom_day);
}
}, year, month, dayOfMonth);
datePickerDialog.show();
}
});
}
}
为什么你也用1来增加月份和年份
下一天就增加
cld.add(Calendar.DATE, 1);
解决方案
calendarImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(PrivacyPolicyActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
// SimpleDateFormat dateandmonth = new SimpleDateFormat("dd-MMM");
int cyear = datePicker.getYear();
int cmonth =datePicker .getMonth();
int cday = datePicker.getDayOfMonth();
Calendar cld = Calendar.getInstance();
cld.set(cyear, cmonth, cday);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
String strDate = format.format(cld.getTime());
todayDate.setText(strDate);
SimpleDateFormat dayfmt = new SimpleDateFormat("EEEE");
String dayselected = dayfmt.format(cld.getTime());
todayDay.setText(dayselected);
cld.add(Calendar.DATE, 1);
SimpleDateFormat day_fmt = new SimpleDateFormat("EEEE");
String tom_day = day_fmt.format(cld.getTime());
tommorrowDay.setText(tom_day);
}
},year,month,dayOfMonth);
datePickerDialog.show();
}
});
为什么你也用1来增加月份和年份
下一天就增加
cld.add(Calendar.DATE, 1);
解决方案
calendarImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePickerDialog = new DatePickerDialog(PrivacyPolicyActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
// SimpleDateFormat dateandmonth = new SimpleDateFormat("dd-MMM");
int cyear = datePicker.getYear();
int cmonth =datePicker .getMonth();
int cday = datePicker.getDayOfMonth();
Calendar cld = Calendar.getInstance();
cld.set(cyear, cmonth, cday);
SimpleDateFormat format = new SimpleDateFormat("dd-MMM");
String strDate = format.format(cld.getTime());
todayDate.setText(strDate);
SimpleDateFormat dayfmt = new SimpleDateFormat("EEEE");
String dayselected = dayfmt.format(cld.getTime());
todayDay.setText(dayselected);
cld.add(Calendar.DATE, 1);
SimpleDateFormat day_fmt = new SimpleDateFormat("EEEE");
String tom_day = day_fmt.format(cld.getTime());
tommorrowDay.setText(tom_day);
}
},year,month,dayOfMonth);
datePickerDialog.show();
}
});
在您的
Datepicker
callback中,放置以下代码以显示第二天
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, datePicker.getDayOfMonth());
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.add(Calendar.DATE, 1);
SimpleDateFormat tom_format = new SimpleDateFormat("dd-MMM");
String tom_date = tom_format.format(calendar);
tommmorrowDate.setText(tom_date);
在您的
Datepicker
callback中,放置以下代码以显示第二天
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE, datePicker.getDayOfMonth());
calendar.set(Calendar.MONTH, datePicker.getMonth());
calendar.set(Calendar.YEAR, datePicker.getYear());
calendar.add(Calendar.DATE, 1);
SimpleDateFormat tom_format = new SimpleDateFormat("dd-MMM");
String tom_date = tom_format.format(calendar);
tommmorrowDate.setText(tom_date);
是的,我试过了。它给了我错误,因为无法解析方法“set(int)”@SushilKumar,如果当前日期是2019年1月31日,那么根据您的代码,下一个日期将是2019年1月32日,对吗?因为,您将日期增加+1。所以,如果当前日期是31,那么第二天将是31+1=32。@ZahidulIslam,这是我刚刚编辑的用户代码。我认为使用日历对Op.@SushilKumar是个好主意,我是说
int nextDay=datePicker.getDayOfMonth()+1代码>行在某些情况下会产生问题。如果datePicker.getDayOfMonth()=31
则datePicker.getDayOfMonth()+1=32
但它应该是下个月的第一天。我想你明白我的意思。您应该使用add(field,value)
方法。@SushilKumar.,是的,我已经用解决方案检查过了。即使我们选择31作为日期,下一个也会自动显示下个月的第一个日期,所以没有问题。是的,我尝试过。它给我错误,因为无法解决方法“set(int)”@SushilKumar,如果当前日期是31-1-2019,那么根据您的代码,下一个日期将是32-1-2019,对吗?因为,您将日期增加+1。所以,如果当前日期是31,那么第二天将是31+1=32。@ZahidulIslam,这是我刚刚编辑的用户代码。我认为使用日历对Op.@SushilKumar是个好主意,我是说int nextDay=datePicker.getDayOfMonth()+1代码>行在某些情况下会产生问题。如果datePicker.getDayOfMonth()=31
则datePicker.getDayOfMonth()+1=32
但它应该是下个月的第一天。我想你明白我的意思。您应该使用add(field,value)
方法。@SushilKumar.,是的,我已经用解决方案检查过了。即使我们选择31作为日期,下一步它也会自动显示下个月的第一个日期,所以没有问题Op的NICE catch错误Op的NICE catch错误