我得到的时间比时间选择器的当前时间早 12 小时
I am getting a time which is 12 hours back than the current time from time picker
我已经为时间选择器编写了代码。我的当前时间选择器显示的时间比实际当前时间少 12 小时。你们中的任何一个人都可以让我知道我在这里错过了什么。欢迎所有建议。提前致谢。
我的时间选择器代码:
public void showTimePicker() {
TimePickerFragment time1 = new TimePickerFragment();
/**
* Set Up Current Time Into dialog
*/
Calendar calendar = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("Hour", calendar.get(Calendar.HOUR));
args.putInt("Minute", calendar.get(Calendar.MINUTE));
time1.setArguments(args);
/**
* Set Call back to capture selected time
*/
time1.setCallBack(onTime);
time1.show(getFragmentManager(), "Time Picker");
}
TimePickerDialog.OnTimeSetListener onTime = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
timeValue1 = time.getText().toString();
}
};
我的时间选择器片段:
public class TimePickerFragment extends DialogFragment {
TimePickerDialog.OnTimeSetListener onTimeSet;
public TimePickerFragment() {
}
public void setCallBack(TimePickerDialog.OnTimeSetListener onTime) {
onTimeSet = onTime;
}
public int hour, minute;
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
hour = args.getInt("Hour");
minute = args.getInt("Minute");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new TimePickerDialog(getActivity(), onTimeSet, hour, minute, true);
}
}
如果您需要更多详细信息,请告诉我。
您正在使用 Calendar.get(Calendar.HOUR)
,其定义如下:
HOUR Field number for get and set indicating the hour of the morning or afternoon.
我认为您需要使用 Calendar.HOUR_OF_DAY
,然后 TimePicker
将处理 am/pm(我假设)。
目前,14:00(下午 2:00)的时间将 return 2 作为小时,因此您失去了 pm/afternoon。
我已经为时间选择器编写了代码。我的当前时间选择器显示的时间比实际当前时间少 12 小时。你们中的任何一个人都可以让我知道我在这里错过了什么。欢迎所有建议。提前致谢。
我的时间选择器代码:
public void showTimePicker() {
TimePickerFragment time1 = new TimePickerFragment();
/**
* Set Up Current Time Into dialog
*/
Calendar calendar = Calendar.getInstance();
Bundle args = new Bundle();
args.putInt("Hour", calendar.get(Calendar.HOUR));
args.putInt("Minute", calendar.get(Calendar.MINUTE));
time1.setArguments(args);
/**
* Set Call back to capture selected time
*/
time1.setCallBack(onTime);
time1.show(getFragmentManager(), "Time Picker");
}
TimePickerDialog.OnTimeSetListener onTime = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.setText(String.valueOf(hourOfDay) + ":" + String.valueOf(minute));
timeValue1 = time.getText().toString();
}
};
我的时间选择器片段:
public class TimePickerFragment extends DialogFragment {
TimePickerDialog.OnTimeSetListener onTimeSet;
public TimePickerFragment() {
}
public void setCallBack(TimePickerDialog.OnTimeSetListener onTime) {
onTimeSet = onTime;
}
public int hour, minute;
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
hour = args.getInt("Hour");
minute = args.getInt("Minute");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new TimePickerDialog(getActivity(), onTimeSet, hour, minute, true);
}
}
如果您需要更多详细信息,请告诉我。
您正在使用 Calendar.get(Calendar.HOUR)
,其定义如下:
HOUR Field number for get and set indicating the hour of the morning or afternoon.
我认为您需要使用 Calendar.HOUR_OF_DAY
,然后 TimePicker
将处理 am/pm(我假设)。
目前,14:00(下午 2:00)的时间将 return 2 作为小时,因此您失去了 pm/afternoon。