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

前端 未结 1 401
旧时难觅i
旧时难觅i 2021-01-25 11:45

In my application I am Using this library for Custom Calender View. On click any date i am passing that date to next activity through shared preferences. I am able to display da

相关标签:
1条回答
  • 2021-01-25 12:04

    I did it. Here is the code for Expected Result. Below is the code from where i am selecting Date and passing it to NextActivity through SharedPreferences.

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

    Here is the next Activity code where getting the date from previous activity using SharedPreferences

     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);
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题