Android CalendarView: How do I get the date in correct format?

前端 未结 4 867
我在风中等你
我在风中等你 2021-01-20 18:26

The past few days I\'ve been searching for ways to get a \'readable\' date out of my calendarview from android 4.0. I can\'t manage to find a solution or example that suits

4条回答
  •  爱一瞬间的悲伤
    2021-01-20 19:01

    kandroidj's answer helps to create date, but not date of correct format. So to format selected date:

    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
    
            @Override
        public void onSelectedDayChange(CalendarView view, int year, int month,
                                        int dayOfMonth) {
            final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            Calendar calendar = Calendar.getInstance();
            calendar.set(year, month, dayOfMonth);
            String sDate = sdf.format(calendar.getTime());
            Log.d(TAG, "sDate formatted: " + sDate);
        }
    });
    

提交回复
热议问题