Java 将整数变量转换为日期对象时出错,返回的值始终为31-DEC-1?

Java 将整数变量转换为日期对象时出错,返回的值始终为31-DEC-1?,java,android,Java,Android,我写了这行代码: dateSelected = new GregorianCalendar(tempYear, tempMonth, tempDate,tempHour,tempMinute).getTime(); 我从android中的datepicker和timepicker对话框中获得了这些值。我能够在这些整数变量中获得正确的值,但当我将它们转换为Date对象时,它将始终是12月31日。我无法解决这个问题 我也尝试过这种方法: Calendar calendar = Calend

我写了这行代码:

dateSelected = new GregorianCalendar(tempYear, tempMonth, tempDate,tempHour,tempMinute).getTime();
我从android中的datepicker和timepicker对话框中获得了这些值。我能够在这些整数变量中获得正确的值,但当我将它们转换为Date对象时,它将始终是12月31日。我无法解决这个问题

我也尝试过这种方法:

    Calendar calendar = Calendar.getInstance();
    calendar.set(tempYear, tempMonth, tempDate, tempHour, tempMinute);
    dateSelected = calendar.getTime();
只要第二行被注释掉,它就可以正常工作。我完全掌握了当前的日期和时间。但当它未注释时,问题又会出现

如果有人需要我的完整代码:

package com.bignerdranch.android.todolistwithdate;

/**********************************************
 * This activity will open new dialog box that will add new
 * ToDo list item to the list.
 */

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class AddToDoActivity extends AppCompatActivity {

    EditText entryName;
    Button dateSelectionButton;
    Button addEntry;
    Date dateSelected;
    Button timeSelectionButton;
    private int tempDate, tempMonth, tempYear, tempHour, tempMinute;
    String nameToDoItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_to_do);

        entryName = (EditText)findViewById(R.id.new_todo_edittext);
        dateSelectionButton = (Button)findViewById(R.id.date_selection_button);
        addEntry = (Button)findViewById(R.id.entry_add_button);
        timeSelectionButton = (Button)findViewById(R.id.time_selection_button);

        //Create date selection Dialog here
        //Also add code for modifying dateSelected Variable
        dateSelectionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment datePickerDialog = new DatePickerFragment() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        super.onDateSet(view, year, monthOfYear, dayOfMonth);
                        dateSelectionButton.setText(Integer.toString(dayOfMonth) + "/" + Integer.toString(monthOfYear + 1) + "/" + Integer.toString(year));
                        tempDate = dayOfMonth;
                        tempMonth = monthOfYear;
                        tempYear = year;
                    }
                };
                datePickerDialog.show(getSupportFragmentManager(), "datePicker");
            }
        });

        timeSelectionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment timePickerDialog = new TimePickerFragment() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        super.onTimeSet(view, hourOfDay, minute);
                        timeSelectionButton.setText(Integer.toString(hourOfDay) + ":" + Integer.toString(minute));
                        tempHour = hourOfDay;
                        tempMinute = minute;
                    }
                };
                timePickerDialog.show(getSupportFragmentManager(), "timePicker");
            }
        });

        //Work here to get calendar working properly
        Calendar calendar = Calendar.getInstance();
        calendar.set(tempYear, tempMonth, tempDate, tempHour, tempMinute);
        dateSelected = calendar.getTime();

        entryName = (EditText)findViewById(R.id.new_todo_edittext);

        addEntry = (Button)findViewById(R.id.entry_add_button);
        addEntry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nameToDoItem = entryName.getText().toString();
                Intent resultIntent = new Intent();
                resultIntent.putExtra("NameOfItem", nameToDoItem);
                resultIntent.putExtra("DateOfItem", dateSelected);
                setResult(Activity.RESULT_OK, resultIntent);

                Log.i("Date: ", dateSelected.toString() );

                finish();
            }
        });
    }
}

当代码运行时,所有变量都等于0,因为用户此时不选择日期和时间

Calendar calendar = Calendar.getInstance();
calendar.set(tempYear, tempMonth, tempDate, tempHour, tempMinute);
dateSelected = calendar.getTime();

addEntry的
onClickListener实现中移动代码,当代码的所有变量都等于0时,它将工作,因为用户此时不选择日期和时间

Calendar calendar = Calendar.getInstance();
calendar.set(tempYear, tempMonth, tempDate, tempHour, tempMinute);
dateSelected = calendar.getTime();
addEntry的
onClickListener实现中移动代码,它就会工作