如何在Material-CalendarView中设置selecteddate

时间:2019-07-02 10:50:18

标签: android

我想从我从表格中获得的数据中添加选定的日期。这是我的数组示例

{"data":[{"Tanggal":"2019-07-02","Flag":1},{"Tanggal":"2019-07-03","Flag":0}]} 

实际上,我不知道该怎么做,我的老师是Google,但我想我错过了一些事情。所以我想像这样写手册

String date = "2019-07-02";
        String parts[] = date.split("-");

        int day = Integer.parseInt(parts[2]);
        int month = Integer.parseInt(parts[1]);
        int year = Integer.parseInt(parts[0]);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);

        long milliTime = calendar.getTimeInMillis();
        widget.setDateSelected(calendar,true);

但我遇到错误

    widget.setDateSelected(calendar,true); // wrong 1st argument type

我要如何实现它,请先谢谢。 顺便说一句,我正在使用这个https://github.com/prolificinteractive/material-calendarview

1 个答案:

答案 0 :(得分:0)

使用此库中的另一种方法setSelectedDate(LocalDate date)设置日期。 这是代码...

try{
    //Option 1
    String myDate = "2019-07-02";

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    Date date = dateFormat.parse(myDate);

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    LocalDate ld = LocalDate.of(cal.get(Calendar.YEAR),
            cal.get(Calendar.MONTH) + 1,
            cal.get(Calendar.DAY_OF_MONTH));

    //Option 2
    LocalDate ld1 = LocalDate.of(2019,07,02);

    //and finally pass it in parameter
    widget.setSelectedDate(ld);//you can also pass ld1

}catch (ParseException e){
    e.printStackTrace();
}

并将以下导入用于LocalDate

import org.threeten.bp.LocalDate;