0

I am looking to get date in dd/mm/yyyy format using Horizontal-calendar (Mulham-Raee library). I get corrent day and year but I cannot figure out to get correct month. My code is :

    //define your start and end dates to set the range of the calendar:

    /* starts before 1 month from now */
    Calendar startDate = Calendar.getInstance();
    startDate.add(Calendar.MONTH, -1);

     final HorizontalCalendar horizontalCalendar;
    /* ends after 1 month from now */
    Calendar endDate = Calendar.getInstance();
    endDate.add(Calendar.MONTH, 1);

    //Then setup HorizontalCalendar in your Activity through its Builder:

     horizontalCalendar = new HorizontalCalendar.Builder(this, R.id.calendarView)
            .range(startDate, endDate)
            .datesNumberOnScreen(5)
            .build();



    horizontalCalendar.setCalendarListener(new HorizontalCalendarListener() {
        @Override
        public void onDateSelected(Calendar date, int position) {
            //do something

            Calendar cal = horizontalCalendar.getDateAt(position);

          cal.add(Calendar.DATE,1);
          SimpleDateFormat format1 = new SimpleDateFormat("dd/mm/yyyy");

          String formatted = format1.format(cal.getTime());
          Log.d("Selected Date", formatted);
        }
    });
B.B.KinG
  • 35
  • 4
  • `SimpleDateFormat` has a pretty good documentation, so you could lookup what `mm` _actually_ means and how you would get Months instead. Voted to close as typo. Or dupe of one of the many "month wrong" questions, like https://stackoverflow.com/questions/8770726/java-date-format-conversion-getting-wrong-month – Tom Oct 18 '19 at 12:22

2 Answers2

2

You have used the small letter 'mm for parsing month. Please use capital 'MM'

new SimpleDateFormat("dd/MM/yyyy");

You can find more about parsing pattern here

Arun P M
  • 352
  • 1
  • 15
2

use this format dd/MM/yyyy intead of dd/mm/yyyy.Let me know if it's works for you.

Jahanvi Kariya
  • 377
  • 3
  • 14