Java 如何将焦点设置在 CalendarView 中的特定日期上,知道日期是“dd/mm/yyyy”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22583122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set focus on a specific date in CalendarView knowing date is "dd/mm/yyyy"
提问by Ilja
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?
我试图弄清楚如何在活动创建期间将焦点设置在 CalendarView 中的特定日期上,该日期是已知的并存储在一个单独的字符串变量中,可以说theDate
并且具有“dd/mm/yyyy
知道这一点”的格式,我可以让日历专注于具体日期?
Assume calendar view is mCalendarView
in this case.
假设日历视图mCalendarView
在这种情况下。
采纳答案by JustSoAmazing
Try this method in CalendarView: http://developer.android.com/reference/android/widget/CalendarView.html#setDate(long)
在 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:
至于在dd/mm/yyyy格式和毫秒格式之间的转换,我建议你以毫秒格式存储日期,显示日期时只转换为dd/mm/yyyy格式。但是,如果您必须使用 dd/mm/yyyy 格式,我会尝试以下操作:
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);
回答by Muhammad Irvan Tommi
this works on me :)
这对我有用:)
String selectedDate = "30/09/2016";
mCalendarView.setDate(new SimpleDateFormat("dd/MM/yyyy").parse(selectedDate).getTime(), true, true);