Justine Mackay Justine Mackay - 25 days ago 4x
Android Question

Android : How can I convert a Time with am and pm format into integer seconds

I have a value in my database with this Time format "10:40 AM" now I'm going to use it to the Alarm Manager this is my alarm manager code.

Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000;


I want to change the 10 inside to the double quote with my "10:40 AM" so I'm going to convert it into integer. How am I going to do this?

Let just say I have "11:40 AM"
I will convert it into integer

int Time = "11:40 AM";

This is the original code then

Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000;

Then this is what I need

Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+Time*1000;

So in order to trigger my alarm manager I need to convert my time into integer.

This is what I've tried so far.

try {
Cursor c = myDB.rawQuery("SELECT * FROM " + "tblEvents", null);

int Column2 = c.getColumnIndex("Date");
int Column3 = c.getColumnIndex("Time");

String[] arr = new String[0];
// Check if our result was valid.
ArrayList<String> list = new ArrayList<String>();
if (c != null) {
// Loop through all Results
do {
String Date = c.getString(Column2);
String Time = c.getString(Column3);
Calendar ca = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("M/dd/yyyy");
String formattedDate = df.format(ca.getTime());

if (Date.equalsIgnoreCase(formattedDate)) {

Toast.makeText(getApplicationContext(), "Converter : ", Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}catch(CursorIndexOutOfBoundsException e){
Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000;


Intent alertIntent = new Intent(this,AlertReceiver.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


You can use a SimpleDateFormat to convert the String into a Date

String myStringDate = "10:40 PM"
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
Date myDate = sdf.parse(myStringDate);

Then get time in milliseconds and replace it in your query:

Calendar calendar = Calendar.getIstance();
int hourToSeconds = calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60;
int minutesToSeconds = calendar.get(Calendar.MINUTE) * 60;

int totalSeconds = hourToSeconds + minutesToSeconds ;
Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+ totalSeconds *1000;