How to set focus on a specific date in CalendarView knowing date is “dd/mm/yyyy”

荒凉一梦 提交于 2020-01-11 07:32:06

问题


I'm trying to figure out how to set a focus on a specific date in CalendarView during activity creation, The date is known and stored in a separate string variable, lets say theDate and has format of dd/mm/yyyy Knowing this, can I make calendar focus on a specific date?

Assume calendar view is mCalendarView in this case.


回答1:


Try this method in CalendarView: http://developer.android.com/reference/android/widget/CalendarView.html#setDate(long)

As for converting between the dd/mm/yyyy format to milliseconds format, I recommend that you store the date in millisecond format and only convert to the dd/mm/yyyy format when displaying the date. If you have to use the dd/mm/yyyy format, though, I would try the following:

    String date = "22/3/2014";
    String parts[] = date.split("/");

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

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

And now set the selected date in the calendar view by doing

    mCalendarView.setDate (milliTime, true, true); 



回答2:


this works on me :)

    String selectedDate = "30/09/2016";
    mCalendarView.setDate(new SimpleDateFormat("dd/MM/yyyy").parse(selectedDate).getTime(), true, true);


来源:https://stackoverflow.com/questions/22583122/how-to-set-focus-on-a-specific-date-in-calendarview-knowing-date-is-dd-mm-yyyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!