我的旧应用程序的某些部分已被弃用,我正在尝试重建它。基本上它是一个带有月 View 的日历。 这是我的 gridview 适配器的一部分:
public View getView(int position, View view, ViewGroup parent) {
Date date = getItem(position);
int day = date.getDate();
int month = date.getMonth();
int year = date.getYear();
}
和 int day = date.getDate(); int month = date.getMonth(); int year = date.getYear();
已弃用。我正在尝试使用 Calendar
类而不是 Date
但无法执行相同的操作。我知道要检索日、月或年,我可以使用它:
Calendar calendar = Calendar.getInstance();
calendar.get(Calendar.DAY_OF_MONTH);
但我不知道如何转换这一行:
Date date = getItem(position);
与 Calendar
一起使用。
最佳答案
您可以使用这行代码:
只需替换这一行
Date date = getItem(position);
用这一行:
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
这里有一个完整的例子:
Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);
关于java - 用于日历 View 的 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43015518/