1

everytime user select date the TextView only showing current date

public void initOverall(){
    TextView dateTest = (TextView)findViewById(R.id.cnvrt);
    CalendarView calendarView = (CalendarView)findViewById(R.id.calendarView);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String selectedDates = sdf.format(new Date(calendarView.getDate()));
            dateTest.setText(selectedDates);

        }
    });
}
Cing Cong
  • 13
  • 5
  • Have you tried to set the date from within the callback to the `CallendarView`? – parohy Sep 04 '18 at 07:32
  • how? sory total noob here – Cing Cong Sep 04 '18 at 07:38
  • 1
    Why you don't use the parameters of the callback ? – Bruno Sep 04 '18 at 07:45
  • According to [Docs](https://developer.android.com/reference/android/widget/CalendarView) you can set the date by calling `setDate` and passing the date to the view. You need to declare your `callendrView` => `final CallendarView callendarView` and inside the callback set `callendarView.setDate(calendarView.getDate())`. This may look absurd and even may cause OOM exeption because it may loop, but worth a try. – parohy Sep 04 '18 at 07:45
  • still same tho, even the background selected date stick to current date – Cing Cong Sep 04 '18 at 08:19
  • im really confused, my program work perfectly on old kitkat samsung tab, but wont work on newer OS – Cing Cong Sep 04 '18 at 08:21
  • what SDK are you using? – parohy Sep 04 '18 at 08:41
  • it work on sdk 19, but not working in sdk 25 and newer – Cing Cong Sep 04 '18 at 09:03
  • the compiler im using sdk 28 – Cing Cong Sep 04 '18 at 09:05
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 04 '18 at 09:45

3 Answers3

3

Because you just selected a date but not got it as onSelectedDayChange stage.

And the date you chosen was saved in parameters of onSelectedDayChange.

You can try the following code :

public void initOverall(){
        final TextView dateTest = (TextView)findViewById(R.id.cnvrt);
        final CalendarView calendarView = (CalendarView)findViewById(R.id.calendarView);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String selectedDates = sdf.format(new Date(year-1900,month,dayOfMonth));
                dateTest.setText(selectedDates);
            }
        });
    }
Seamas
  • 46
  • 4
  • wow thanks its works,kind sir dare you explain to me what the function of "year-1900"? – Cing Cong Sep 04 '18 at 09:18
  • Press your ctrl and click Date, you will see some information in source code of Android Studio. I guess CalendarView doesn't need to show the date before 1900. – Seamas Sep 04 '18 at 09:28
  • Thanks for wanting to contribute. Apart from using the old-fashioned classes `Date` and `SimpleDateFormat` you are also using the deprecated three-arg `Date` constructor. Never do that. It was deprecated because it works unreliably across time zones. – Ole V.V. Sep 04 '18 at 09:47
0

java.time

public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
    LocalDate date = LocalDate.of(year, Month.values()[month], dayOfMonth);
    String selectedDates = date.toString();
    dateTest.setText(selectedDates);
}

As has been said in comments, you need to pick up the date that the user has chosen from the arguments passed to onSelectedDayChange. Unfortunately the month passed is 0-based, a number in the interval from 0 for January through 11 for December. Month.values()[month] is my trick for obtaining the correct Month object. If you prefer, you may instead just add 1:

    LocalDate date = LocalDate.of(year, month + 1, dayOfMonth);

I am further exploiting the fact that LocalDate.toString produces the format you want, so we need no explicit formatter. The format is ISO 8601.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
-1
CalendarView v = new CalendarView( this );
 v.setOnDateChangeListener( new CalendarView.OnDateChangeListener() {
    public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
       this.calendar = new GregorianCalendar( year, month, dayOfMonth );
    }//met
 });
Erselan Khan
  • 692
  • 6
  • 13