1970年Datepicker日期

时间:2013-01-30 13:35:43

标签: java android datepicker

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

2 个答案:

答案 0 :(得分:0)

日期'1970年1月1日'是默认值,因为它从'1970年1月1日'到今天开始计算毫秒数。所以当你从数据库中获取日期或解析它时,我猜错了。你能检查解析是否会引发异常吗?

答案 1 :(得分:0)

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

不应该

dateToEditApp = newFormat.parse(returnedDate);

dateToEditApp = newFormatDate.parse(returnedDate);

相关问题