首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Android中的datepicker对话框中显示选定日期中的第二天日期?

如何在Android中的datepicker对话框中显示选定日期中的第二天日期?
EN

Stack Overflow用户
提问于 2019-01-08 06:15:37
回答 2查看 978关注 0票数 0

我目前正在显示今天的日期和日期&以及明天的日期和日期在单独的布局,而在页面加载。但我的要求是

1)无论我从Datepicker,中选择什么日期,它都应该显示在第一个布局上

2)第二天的日期应显示在第二版面上。

我的第一个要求是工作正常,没有任何问题。(即),无论我选择哪一天,它都显示在第一版面上。没问题,但我的第二个要求有问题。

如果我选择在DatePicker,上选择25-5月-2018年,它将显示在第一版面上,而不是将第二天的日期显示为26-5-2018,而是显示为26-6-6-2018。但我需要它的工作方式,如果我选择25-5月-2018年在Datepicker,,选定的日期应该显示在第一布局&第二天的第二天(26-5-2018)应该显示在下一个布局。这应该适用于我在Datepicker.中选择的任何日期

我哪里出错了?

这是我的Activity.Java:

代码语言:javascript
复制
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();
            }

        });
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-08 06:27:48

为什么你的月份和年份也增加了1?

代码语言:javascript
复制
int nextYear = datePicker.getYear() + 1;
int nextMonth = datePicker.getMonth() + 1;
int nextDay = datePicker.getDayOfMonth() + 1;

只需增加nextDay。

代码语言:javascript
复制
cld.add(Calendar.DATE, 1);

解决方案

代码语言:javascript
复制
 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();
            }





        });
票数 2
EN

Stack Overflow用户

发布于 2019-01-08 06:39:52

在您的Datepicker回调中,放置下面的代码,以便在第二天显示。

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54086146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档