简体   繁体   中英

java Calendar get hour before the next day

I have a date : 21/08/2013 17:05:06 And I would like to know the number of milliseconds before 22/08/2013 00:00:00

Is it possible to do that with Calendar object please ? Thanks

EDIT :

My solution found :

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());

为每个日期创建一个日历对象,然后减去其getTimeInMillis()

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();

You can set the alarm by using this method:

/**
     * 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);
    }

And the Helper Method

   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;
    }

In the setAlarm method we could see that we add 1 day if our current time is already passed away, therefore we can ensure that time will will prompt on the next day around.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM