计算两个日期选择器之间的秒差
Calculate different seconds between two Timepickers
我试着遵循这里的建议:
android calculating time difference for setting alarm
这就是我所做的:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, timeSchedule.getCurrentHour());
start.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, toTime.getCurrentHour());
end.set(Calendar.MINUTE, toTime.getCurrentMinute());
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);
其中 toTime
和 timeSchedule
是 TimePicker
。但是,结果不正确。我怀疑是因为 AM
和 PM
.
请帮助我,非常感谢你的帮助。
更新
感谢@Raghunandan,这是我当前的代码:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, timeSchedule.getCurrentHour());
start.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());
if (start.get(Calendar.AM_PM) == Calendar.AM)
start.set(Calendar.AM_PM, Calendar.AM);
else
start.set(Calendar.AM_PM, Calendar.PM);
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR, toTime.getCurrentHour());
end.set(Calendar.MINUTE, toTime.getCurrentMinute());
if (end.get(Calendar.AM_PM) == Calendar.AM)
end.set(Calendar.AM_PM, Calendar.AM);
else
end.set(Calendar.AM_PM, Calendar.PM);
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);
现在如果两个 Timpickers 都是上午或下午,结果为真。但是,如果用户将晚上 7 点设置为凌晨 1 点,则结果将为假(<0 或负值)。我该怎么办?
尝试
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, 6);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.AM_PM,Calendar.AM);
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR, 6); // 12 hour format
end.set(Calendar.MINUTE, 30);
end.set(Calendar.AM_PM,Calendar.PM); // AM or PM
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis) - TimeUnit.MILLISECONDS.toSeconds(startMillis) ;
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) // 12 hour diff
使用 Calendar.Hour
表示 12 小时制,Calendar.HOUR_OF_DAY
表示 24 小时制。
http://developer.android.com/reference/java/util/Calendar.html#AM_PM
http://developer.android.com/reference/java/util/Calendar.html#HOUR
编辑:
晚上 7 点到次日凌晨 1 点
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, 7);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.AM_PM,Calendar.PM);
Calendar end = Calendar.getInstance();
int day = (start.get(Calendar.DAY_OF_MONTH)+1);
end.set(Calendar.DAY_OF_MONTH,day);
end.set(Calendar.HOUR, 1);
end.set(Calendar.MINUTE, 30);
end.set(Calendar.AM_PM,Calendar.AM);
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis) - TimeUnit.MILLISECONDS.toSeconds(startMillis) ;
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) ;
编辑 2:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, 21);
start.set(Calendar.MINUTE, 30);
if (start.get(Calendar.AM_PM) == Calendar.AM)
start.set(Calendar.AM_PM, Calendar.AM);
else
start.set(Calendar.AM_PM, Calendar.PM);
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, 4);
end.set(Calendar.MINUTE, 30);
int day = (start.get(Calendar.DAY_OF_MONTH)+1);
end.set(Calendar.DAY_OF_MONTH,day);
if (end.get(Calendar.AM_PM) == Calendar.AM)
end.set(Calendar.AM_PM, Calendar.AM);
else
end.set(Calendar.AM_PM, Calendar.PM);
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) ;
Toast.makeText(this,""+diffHour, Toast.LENGTH_LONG).show();
注意:您可能还需要日期选择器。
我试着遵循这里的建议:
android calculating time difference for setting alarm
这就是我所做的:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, timeSchedule.getCurrentHour());
start.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, toTime.getCurrentHour());
end.set(Calendar.MINUTE, toTime.getCurrentMinute());
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);
其中 toTime
和 timeSchedule
是 TimePicker
。但是,结果不正确。我怀疑是因为 AM
和 PM
.
请帮助我,非常感谢你的帮助。
更新
感谢@Raghunandan,这是我当前的代码:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, timeSchedule.getCurrentHour());
start.set(Calendar.MINUTE, timeSchedule.getCurrentMinute());
if (start.get(Calendar.AM_PM) == Calendar.AM)
start.set(Calendar.AM_PM, Calendar.AM);
else
start.set(Calendar.AM_PM, Calendar.PM);
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR, toTime.getCurrentHour());
end.set(Calendar.MINUTE, toTime.getCurrentMinute());
if (end.get(Calendar.AM_PM) == Calendar.AM)
end.set(Calendar.AM_PM, Calendar.AM);
else
end.set(Calendar.AM_PM, Calendar.PM);
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);
现在如果两个 Timpickers 都是上午或下午,结果为真。但是,如果用户将晚上 7 点设置为凌晨 1 点,则结果将为假(<0 或负值)。我该怎么办?
尝试
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, 6);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.AM_PM,Calendar.AM);
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR, 6); // 12 hour format
end.set(Calendar.MINUTE, 30);
end.set(Calendar.AM_PM,Calendar.PM); // AM or PM
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis) - TimeUnit.MILLISECONDS.toSeconds(startMillis) ;
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) // 12 hour diff
使用 Calendar.Hour
表示 12 小时制,Calendar.HOUR_OF_DAY
表示 24 小时制。
http://developer.android.com/reference/java/util/Calendar.html#AM_PM
http://developer.android.com/reference/java/util/Calendar.html#HOUR
编辑:
晚上 7 点到次日凌晨 1 点
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, 7);
start.set(Calendar.MINUTE, 30);
start.set(Calendar.AM_PM,Calendar.PM);
Calendar end = Calendar.getInstance();
int day = (start.get(Calendar.DAY_OF_MONTH)+1);
end.set(Calendar.DAY_OF_MONTH,day);
end.set(Calendar.HOUR, 1);
end.set(Calendar.MINUTE, 30);
end.set(Calendar.AM_PM,Calendar.AM);
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis) - TimeUnit.MILLISECONDS.toSeconds(startMillis) ;
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) ;
编辑 2:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR_OF_DAY, 21);
start.set(Calendar.MINUTE, 30);
if (start.get(Calendar.AM_PM) == Calendar.AM)
start.set(Calendar.AM_PM, Calendar.AM);
else
start.set(Calendar.AM_PM, Calendar.PM);
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR_OF_DAY, 4);
end.set(Calendar.MINUTE, 30);
int day = (start.get(Calendar.DAY_OF_MONTH)+1);
end.set(Calendar.DAY_OF_MONTH,day);
if (end.get(Calendar.AM_PM) == Calendar.AM)
end.set(Calendar.AM_PM, Calendar.AM);
else
end.set(Calendar.AM_PM, Calendar.PM);
long startMillis = start.getTimeInMillis();
long endMillis = end.getTimeInMillis();
long diffSec = TimeUnit.MILLISECONDS.toSeconds(endMillis - startMillis);
long diffHour = TimeUnit.MILLISECONDS.toHours(endMillis) - TimeUnit.MILLISECONDS.toHours(startMillis) ;
Toast.makeText(this,""+diffHour, Toast.LENGTH_LONG).show();
注意:您可能还需要日期选择器。