【发布时间】:2016-09-26 15:22:33
【问题描述】:
我的数据库中有一个时间格式为“10:40 AM”的值,现在我要将它用于警报管理器,这是我的警报管理器代码。
Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000;
+10*1000
我想用“10:40 AM”将内部的 10 更改为双引号,因此我将其转换为整数。我该怎么做?
假设我有“上午 11:40” 我会把它转换成整数
int 时间 = "11:40 AM";
这是原来的代码
Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000;
那么这就是我需要的
Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+Time*1000;
所以为了触发我的警报管理器,我需要将我的时间转换为整数。
这是我迄今为止尝试过的。
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>();
c.moveToFirst();
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)) {
list.add(Time);
Toast.makeText(getApplicationContext(), "Converter : ", Toast.LENGTH_SHORT).show();
}
} while (c.moveToNext());
}
Collections.reverse(list);
}catch(CursorIndexOutOfBoundsException e){
e.printStackTrace();
}
Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000;
Toast.makeText(getApplicationContext(),alertTimeCatcher.toString(),Toast.LENGTH_SHORT).show();
Intent alertIntent = new Intent(this,AlertReceiver.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,alertTimeCatcher,PendingIntent.getBroadcast(this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT));
【问题讨论】: