简体   繁体   中英

Gettting date in dd/mm/yyyy format using Horizontal-Calendar library (Mulham-Raee/Horizontal-Calendar) calendar

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM