我有个约会日期:21/08/2013 17:05:06 我想知道22/08/2013 00:00:00之前的毫秒数
请问可以用Calendar对象吗? 感谢
编辑:
我的解决方案发现:
Calendar cal2 = Calendar.getInstance(TimeZone.getDefault());
cal2.setTimeInMillis(prevCalBatt.getTimeInMillis());
cal2.add(Calendar.DAY_OF_MONTH,1); //add a day
cal2.set(Calendar.HOUR_OF_DAY, 0); //set time to midnight
cal2.set(Calendar.MINUTE, 0);
cal2.set(Calendar.SECOND, 0);
cal2.set(Calendar.MILLISECOND, 0);
timeBetweenBoth+=(cal2.getTimeInMillis()-prevCalBatt.getTimeInMillis());
答案 0 :(得分:1)
为每个日期创建一个日历对象,然后减去他们的getTimeInMillis()
。
答案 1 :(得分:0)
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(sdf.parse("22/08/2013 00:00:00"));
long millis = cal.getTimeInMillis();
cal.setTime(sdf.parse("21/08/2013 17:05:06"));
millis -= cal.getTimeInMillis();
答案 2 :(得分:0)
您可以使用以下方法设置闹钟:
/**
* Set the Alarm
*
* @param context the activity context
* @param id the alarm ID for this app
* @param hour the alarm hour
* @param minute the alarm minute
* @param timeZone the timezone "am" = Calendar.AM or "pm" = Calendar.PM
*/
public static void setAlarm(Context context, int id, int hour, int minute, String timeZone) {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
AlarmStateCache cache = AlarmStateCache.getInstance(context);
cache.saveState(State.ON);
Calendar now = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, to24Hour(hour, timeZone));
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
if(calendar.before(now))
calendar.add(Calendar.DAY_OF_MONTH,1);
Intent intent = new Intent(context, YourReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(context, id, intent, 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 20, pIntent);
}
辅助方法
public static int to24Hour(int hour, String zone) {
if (1 > hour && hour < 12)
throw new IllegalArgumentException("Hour must only be 1 - 12!");
return zone.equals("am") ? hour : hour + 12;
}
在setAlarm
方法中,我们可以看到,如果我们当前的时间已经过去,我们会增加1天,因此我们可以确保第二天的时间会提示。