gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:38 24 4
gpt4 key购买 nike

我写了这行代码:

dateSelected = new GregorianCalendar(tempYear, tempMonth, tempDate,tempHour,tempMinute).getTime();

我从 android 中的日期选择器和时间选择器对话框中获取了这些值。我能够在这些整数变量中获得正确的值,但是当我将它们转换为 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 实现中,它将起作用 👍

关于java - 将整数变量转换为日期对象时出错,返回值始终为 31-DEC - 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35778635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com