0

我正在尝试使用 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);
4

2 回答 2

1

您的代码可能存在一个小问题,我认为您在尝试解析它时将 newFormatDate 命名错误,这将转到 dateToEditApp 的默认值(即 1-1-1970 日期)

不应该

dateToEditApp = newFormat.parse(returnedDate);

dateToEditApp = newFormatDate.parse(returnedDate);

?

于 2013-01-30T13:45:27.253 回答
0

日期“1970 年 1 月 1 日”是默认值,因为它从“1970 年 1 月 1 日”到今天开始计算毫秒数。所以我想当你从数据库中获取日期或解析它时,出了点问题。您可以检查解析是否引发异常?

于 2013-01-30T13:43:51.057 回答