如何在Android Studio中选定的日期添加天数

发布于 2025-01-19 21:06:54 字数 1668 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2025-01-26 21:06:54

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

    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();
        }
    });

I found the solution and if there is another proposal it is with pleasure:

    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();
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文