Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何将am和pm格式的时间转换为整数秒_Java_Android - Fatal编程技术网

Java Android:如何将am和pm格式的时间转换为整数秒

Java Android:如何将am和pm格式的时间转换为整数秒,java,android,Java,Android,我的数据库中有一个时间格式为“10:40 AM”的值,现在我要将它用于报警管理器这是我的报警管理器代码 Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000; +10*1000 我想用我的“10:40 AM”把里面的10改成双引号,所以我要把它转换成整数。我该怎么做 就说我有“上午11:40” 我将把它转换成整数 国际时间=“上午11:40” 这是原始代码 Long alertTimeCatcher

我的数据库中有一个时间格式为“10:40 AM”的值,现在我要将它用于报警管理器这是我的报警管理器代码

Long alertTimeCatcher = new GregorianCalendar().getTimeInMillis()+10*1000;
+10*1000
我想用我的“10:40 AM”把里面的10改成双引号,所以我要把它转换成整数。我该怎么做

就说我有“上午11:40” 我将把它转换成整数

国际时间=“上午11:40”

这是原始代码

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));
试试看{
游标c=myDB.rawQuery(“从“+”tblEvents中选择*”,null);
int Column2=c.getColumnIndex(“日期”);
int Column3=c.getColumnIndex(“时间”);
字符串[]arr=新字符串[0];
//检查我们的结果是否有效。
ArrayList=新建ArrayList();
c、 moveToFirst();
如果(c!=null){
//循环浏览所有结果
做{
字符串日期=c.getString(第2列);
字符串时间=c.getString(第3列);
Calendar ca=Calendar.getInstance();
SimpleDataFormat df=新的SimpleDataFormat(“M/dd/yyyy”);
字符串formattedDate=df.format(ca.getTime());
if(日期相等信号案例(格式化日期)){
列表。添加(时间);
Toast.makeText(getApplicationContext(),“Converter:”,Toast.LENGTH_SHORT.show();
}
}而(c.moveToNext());
}
收款。反向(列表);
}捕获(游标索引自动边界异常e){
e、 printStackTrace();
}
Long alertTimeCatcher=new GregorianCalendar().getTimeInMillis()+10*1000;
Toast.makeText(getApplicationContext(),alertTimeCatcher.toString(),Toast.LENGTH_SHORT).show();
Intent-alertIntent=新的Intent(这是AlertReceiver.class);
AlarmManager AlarmManager=(AlarmManager)getSystemService(Context.ALARM\u服务);
alarmManager.set(alarmManager.RTC_唤醒、alertTimeCatcher、PendingEvent.getBroadcast(this、1、alertIntent、PendingEvent.FLAG_更新_CURRENT));
又快又脏:(+这是一条单行线)


执行此操作的步骤非常简单:

  • 找出是
    AM
    还是
    PM
  • 找出该语句的时数和分钟数(
    substring
    indexOf
    将很有帮助)
  • 如果是下午,加上12小时
  • 将小时乘以
    60*60
    ,得到秒数
  • 将分钟数乘以
    60
    得到秒数
之后,您将时间字符串计算为整数秒


格式良好且可读的代码将是:

boolean isPM = timeStr.endsWith("PM");
int hours = Integer.parseInt(timeStr.substring(0, timeStr.indexOf(":")));
if (isPM) hours += 12;
int minutes = Integer.parseInt(timeStr.substring(timeStr.indexOf(":") + 1, timeStr.indexOf(" ")));
您可以使用将
字符串
转换为
日期

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;
你可以用

SimpleDataFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。它允许格式化(日期->文本)、解析(文本->日期)和规范化


可能与@PierGiorgioMisley重复是的,但在本例中我没有使用任何时间格式化程序(当然,但这是一个较长且较慢的解决方案;)1.我不认为这是一个“更长”的解决方案。。。2.我提供的解决方案比仅凭我的意见使用日历的解决方案快30倍左右。。你有一个解析,一个sumstring,一个indexof,加上一个endswith,再加上一个if,再加上另一个indexof的另一个子串,再加上另一个indexof。我们认为它比新的SimpleDataFormat(“HH:mm aa”)更令人困惑?这里不是讨论的地方。。。如果你想的话,我们可以在聊天中这样做,但是:当然,当你做一行时,它看起来很混乱,把它分开,就可以了。这样做要快得多
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;
String strDate = "10:40 PM"
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm aa");
Date date = simpleDateFormat.parse(strDate);