我正在尝试使用 OnCreate 方法存储在我的数据库中的检索到的日期值来设置我的应用程序的日期选择器。一切都很好,直到我自己用年、月和日值设置日期选择器。目前,日期选择器始终设置为“1970 年 1 月 1 日”。希望有人可以在这里为我指明正确的方向。
如图所示,我将字符串解析为 Date 对象,然后检索单独的年、月和日值。它们存储为整数,然后使用 .init 方法设置我的日期选择器。
到目前为止,这是我的代码:
DBHandlerApp dateToEdit= new DBHandlerApp(this, null, null);
dateToEdit.open();
String returnedDate = dateToEdit.getTime(passedID);
SimpleDateFormat newFormatDate = new SimpleDateFormat("dd-MMM-yyyy");
try
{
dateToEditApp = newFormat.parse(returnedDate);
} catch (ParseException e)
{
e.printStackTrace();
}
Calendar calDate = Calendar.getInstance();
calDate.setTime(dateToEditApp);
int year = calDate.get(Calendar.YEAR);
int monthOfYear = calDate.get(Calendar.MONTH);
int dayOfMonth = calDate.get(Calendar.DAY_OF_MONTH);
editDatePicker.init(year, monthOfYear, dayOfMonth, null);