java - 安卓 : How can I convert a Time with am and pm format into integer seconds

标签 java android

我的数据库中有一个时间格式为“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));

最佳答案

您可以使用 SimpleDateFormatString 转换为 Date

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

然后获取以毫秒为单位的时间并将其替换到您的查询中:

Calendar calendar = Calendar.getIstance();
calendar.setTime(myDate);
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;

关于java - 安卓 : How can I convert a Time with am and pm format into integer seconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39706638/

相关文章:

android - Android Studio图像 Assets 启动器图标透明背景色

java - 来自 java 的 asp.NET 帖子

java - 将 JSON 设置为 EditText

java - HTTP 状态 404 错误 Java Servlet

java.lang.NoClassDefFoundError : Failed resolution of: Ljavax/naming/directory/InitialDirContext; 错误

java - 使用 EL <C :forEach> 输出 JCoTable 行

android - 如何在 Uber 和 Lyft 等 map 上附加灵活的标记?

android - 如何在新版 Android Studio 的模拟器中配置代理?

java - NullPointerException 使用 getWindowManager()

java - Eclipse Draw2d。更改 IFigure 的 z 顺序