Android使用Android日期选择器在Parse.com上保存和检索日期

时间:2015-04-11 23:56:21

标签: android android-studio parse-platform datepicker android-datepicker

编辑:我现在使用以下代码获取日期,并且我希望将所选日期放入解析日期列(字符串日期变量):

private void setDateTimeField() {
    deadlineDateEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            datePickerDialog.show();
        }
    });

    Calendar newCalendar = Calendar.getInstance();
    datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            deadlineDateEditText.setText(dateFormatter.format(newDate.getTime()));
            //get string version of date
            date = dateFormatter.format(newDate.getTime());
        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}

我的问题是如何在没有时间的情况下将它添加到Parse中的Date列?我还没有遇到Android的任何示例。我也关注Parse对日期类型所需的格式。

当我尝试使用以下代码输入日期时,我得到parse android expected date but got String。代码:

public void addModuleDeadline()
{
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Module");
    query.whereEqualTo("moduleCode", moduleDeadlineSpinnerText);
    query.getFirstInBackground(new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
            if (e == null)
            {
                ParseObject deadlineObject = new ParseObject("ModuleDeadlines");
                deadlineObject.put("moduleCode", moduleDeadlineSpinnerText);
                deadlineObject.put("createdBy", currentUser);
                deadlineObject.put("deadlineTitle", deadlineTitleText);
                deadlineObject.put("deadline", date);
                deadlineObject.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e != null) {
                            Toast.makeText(getActivity(), "Error adding deadline: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getActivity(), "Deadline added to '" + moduleDeadlineSpinnerText + "'", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
            else
            {
                Toast.makeText(getActivity(), "There was an issue adding the deadline", Toast.LENGTH_LONG).show();
            }
        }
    });
}

据我了解,您可以使用object.getDate("column name")从Parse获取日期。

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

通过简单地将deadlineObject.put("deadline", date);更改为传递日期对象而不是字符串的deadlineObject.put("deadline", newDate.getTime());来管理解决此问题。

希望这有助于其他任何人!