Calendar.set(field,value) 没有正确设置小时
The Calendar.set(field,value) doesn't set the hour properly
我在 android 应用程序中使用了 2 个不同的对话框选择器,一个用于日期,一个用于小时。
一旦用户确认了对话框中的值 selected,我就使用它们来使用函数 cal.set()
更新变量 Calendar cal
。但是,当我在使用 cal.getTime()
设置时间后尝试获取时间时,输出不一致。即,如果我从选择器 select 6:00 更新我的校准,有时小时输出是 18:00,有时是 6:00。我完全不明白的另一件奇怪的事情是,即使我不更改实际的 DAY 字段,在 6 A.M(6) 和 6 P.M (18) 之间交换有时也会增加DAY 计数。
测试我已经看到来自 timePicker 的值正是我点击它们的值。
cal
变量是私有的,它在与休闲代码相同的 activity class 中声明。
日历根据打开应用程序的意图以 2 种不同的方式实例化,在一种情况下我得到当前时间 cal = Calendar.getInstance();
否则在我 getInstance() 之后我设置使用 cal.setTime(date);
timeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int inputHH, int inputMM) {
Log.d("Output picker hour ",String.valueOf(inputHH));
Log.d("Output picker minutes ",String.valueOf(inputMM));
cal.set(Calendar.MINUTE, inputMM);
cal.set(Calendar.HOUR, inputHH);
Log.d("calendar hour", cal.getTime().toString());
timeView.setText(hourFormat.format(cal.getTime()));
}
这是我在测试期间的控制台输出
D/Output picker hour: 6
D/Output picker minutes: 0
D/calendar hour: Sun Jun 07 18:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f466c900 (RippleDrawable) with handle 0x730f4f84e0
D/Output picker hour: 18
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f5734100 (RippleDrawable) with handle 0x72f4ee58e0
D/Output picker hour: 6
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f5537a00 (RippleDrawable) with handle 0x72f47bc4a0
D/Output picker hour: 6
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f5596100 (RippleDrawable) with handle 0x72f5fec360
D/Output picker hour: 18
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 18:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f4248400 (RippleDrawable) with handle 0x72f467afa0
D/Output picker hour: 18
D/Output picker minutes: 0
D/calendar hour: Tue Jun 09 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f42f4700 (RippleDrawable) with handle 0x72f56b33c0
使用
cal.set(Calendar.HOUR_OF_DAY, inputHH);
改为
cal.set(Calendar.HOUR, inputHH);
我在 android 应用程序中使用了 2 个不同的对话框选择器,一个用于日期,一个用于小时。
一旦用户确认了对话框中的值 selected,我就使用它们来使用函数 cal.set()
更新变量 Calendar cal
。但是,当我在使用 cal.getTime()
设置时间后尝试获取时间时,输出不一致。即,如果我从选择器 select 6:00 更新我的校准,有时小时输出是 18:00,有时是 6:00。我完全不明白的另一件奇怪的事情是,即使我不更改实际的 DAY 字段,在 6 A.M(6) 和 6 P.M (18) 之间交换有时也会增加DAY 计数。
测试我已经看到来自 timePicker 的值正是我点击它们的值。
cal
变量是私有的,它在与休闲代码相同的 activity class 中声明。
日历根据打开应用程序的意图以 2 种不同的方式实例化,在一种情况下我得到当前时间 cal = Calendar.getInstance();
否则在我 getInstance() 之后我设置使用 cal.setTime(date);
timeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int inputHH, int inputMM) {
Log.d("Output picker hour ",String.valueOf(inputHH));
Log.d("Output picker minutes ",String.valueOf(inputMM));
cal.set(Calendar.MINUTE, inputMM);
cal.set(Calendar.HOUR, inputHH);
Log.d("calendar hour", cal.getTime().toString());
timeView.setText(hourFormat.format(cal.getTime()));
}
这是我在测试期间的控制台输出
D/Output picker hour: 6
D/Output picker minutes: 0
D/calendar hour: Sun Jun 07 18:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f466c900 (RippleDrawable) with handle 0x730f4f84e0
D/Output picker hour: 18
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f5734100 (RippleDrawable) with handle 0x72f4ee58e0
D/Output picker hour: 6
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f5537a00 (RippleDrawable) with handle 0x72f47bc4a0
D/Output picker hour: 6
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f5596100 (RippleDrawable) with handle 0x72f5fec360
D/Output picker hour: 18
D/Output picker minutes: 0
D/calendar hour: Mon Jun 08 18:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f4248400 (RippleDrawable) with handle 0x72f467afa0
D/Output picker hour: 18
D/Output picker minutes: 0
D/calendar hour: Tue Jun 09 06:00:50 GMT+02:00 2020
D/OpenGLRenderer: endAllActiveAnimators on 0x72f42f4700 (RippleDrawable) with handle 0x72f56b33c0
使用
cal.set(Calendar.HOUR_OF_DAY, inputHH);
改为
cal.set(Calendar.HOUR, inputHH);