使用 SharedPreferences android 从上一个 activity 获取日期后,获取 previous/next 天按钮按下日期

get previous/next day Date on button press after getting date from previous activity using SharedPreferences android

在我的应用程序中,我使用 this library 自定义日历视图。单击任何日期,我将通过共享首选项将该日期传递给下一个 activity。我可以在下一个 activity 中显示日期。现在我想要的是有两个按钮,一个是"Previous",另一个是"Next"。如果我点击任何按钮,日期必须根据要求更改。

点击日期代码是

calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() {
        @Override
        public void onDateClick(@NonNull Date selectedDate) {
            SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            String newselectedDate=df_new.format(selectedDate);
            Toast.makeText(getApplicationContext(), "You selected" + newselectedDate + " : Date", Toast.LENGTH_SHORT).show();

            // SharedPreferences for sharing the Date with Next Activity, Added by Tara
            SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); String sdd=s_day.format(selectedDate);
            SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());String smm=s_month.format(selectedDate);
            SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());String syy=s_year.format(selectedDate);
            SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE);
            SharedPreferences.Editor spe = spf.edit();
          //  spe.putString("sdate", String.valueOf(selectedDate));
            spe.putString("sday", String.valueOf(sdd));
            spe.putString("smonth", String.valueOf(smm));
            spe.putString("syear", String.valueOf(syy));
            Log.d("Day is", String.valueOf(sdd));
            Log.d("Month is", String.valueOf(smm));
            Log.d("Year  is", String.valueOf(syy));

            spe.commit();


            Intent i_available_slot=new Intent(BookAnAppoinment.this, ChangeDate.class);
            startActivity(i_available_slot);
        }
    });

和下一个 Activity 我通过共享首选项检索日期的地方是

SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
    String id1 = spf.getString("sday", "no value");
    String id2 = spf.getString("smonth", "no value");
    String id3 = spf.getString("syear", "no value");

    String sdate=id1+"-"+id2+"-"+id3;
  //  d.setText(sdate);

    final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    final String formattedDate = df.format(sdate);

    d.setText(formattedDate);

    try {
        Date date=df.parse(sdate);
        Calendar cal = df.getCalendar();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    p.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

我写了 try/catch 块,但无法按照我的要求进行编码。请帮帮我。

我做到了。这是预期结果的代码。下面是我选择日期并将其通过 SharedPreferences 传递给 NextActivity 的代码。

calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() {
        @Override
        public void onDateClick(@NonNull Date selectedDate) {
            SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            String newselectedDate=df_new.format(selectedDate);
            Toast.makeText(getApplicationContext(),"You selected" +newselectedDate +" : Date",Toast.LENGTH_SHORT).show();
            Log.v("You Selected Date is :",newselectedDate);

            // SharedPreferences for sharing the Date with Next Activity, Added by Tara
            SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); int sdd= Integer.parseInt(s_day.format(selectedDate));
            SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());int smm= Integer.parseInt(s_month.format(selectedDate));
            SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());int syy= Integer.parseInt(s_year.format(selectedDate));
            SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE);
            SharedPreferences.Editor spe = spf.edit();
            //  spe.putString("sdate", String.valueOf(selectedDate));
            spe.putInt("sday", sdd);
            spe.putInt("smonth", smm);
            spe.putInt("syear", syy);
            Log.d("Day is", String.valueOf(sdd));
            Log.d("Month is", String.valueOf(smm));
            Log.d("Year  is", String.valueOf(syy));

            spe.commit();

            Intent i_available_slot=new Intent(MainActivity.this, ChangeDate.class);
            startActivity(i_available_slot);
        }
    });

这是下一个 Activity 代码,其中使用 SharedPreferences

从上一个 activity 获取日期
 SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
    final int id1 = spf.getInt("sday", 0);
    final int id2 = spf.getInt("smonth", 0);
    final int id3 = spf.getInt("syear", 0);

    sdate= id1+"-"+id2+"-"+id3;
    date.setText(sdate);
    Log.v("Spf Day :", String.valueOf(id1));
    Log.v("Spf Month :", String.valueOf(id2));
    Log.v("Spf Year :", String.valueOf(id3));

    int month_index=id2-1;  // Java Maintains Index Number from 0 so reduce the month to 1 to get the selected month

    final Calendar c=Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());
   // c.set(id1, id2, id3);
    c.set(Calendar.YEAR,id3);
    c.set(Calendar.MONTH,month_index);
    c.set(Calendar.DAY_OF_MONTH,id1);

    final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    formattedDate = df.format(c.getTime());
    Log.v("FormatedDate:",formattedDate);

    prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            c.add(Calendar.DATE, -1);
            formattedDate = df.format(c.getTime());

            Log.v("PREVIOUS DATE : ", formattedDate);
            date.setText(formattedDate);

        }
    });
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            c.add(Calendar.DATE, 1);
            formattedDate = df.format(c.getTime());

            Log.v("PREVIOUS DATE : ", formattedDate);
            date.setText(formattedDate);

        }
    });