Search code examples
androidcalendaralarmmanagermilliseconds

Android Milliseconds as of a time


I have read all of the docs and there doesnt seem to be too much to really explains the date functions, or the lack there of.

I am trying implement the AlarmManger which needs the time in milliseconds (ms) for the trigger. To test I took the current time and added 5 seconds and that was good.

// get a Calendar object with current time
 Calendar cal = Calendar.getInstance();
 // add 5 minutes to the calendar object
 cal.add(Calendar.SECOND, 5);

If I have a date and time how would I get the ms for that time.

Like "3/2/2011 08:15:00"

How do I turn that into milliseconds?


Solution

  • Use this method.

    example:

    method call for 3/2/2011 08:15:00

    D2MS( 3, 2, 2011, 8, 15, 0);
    

    method

    public long D2MS(int month, int day, int year, int hour, int minute, int seconds) { 
        Calendar c = Calendar.getInstance();
        c.set(year, month, day, hour, minute, seconds);
    
        return c.getTimeInMillis();  
    }