【问题标题】:Android : How can I convert a Time with am and pm format into integer secondsAndroid : 如何将 am 和 pm 格式的时间转换为整数秒
【发布时间】: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));

【问题讨论】:

标签: java android


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以使用SimpleDateFormat

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

    String strDate = "10:40 PM"
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm aa");
    Date date = simpleDateFormat.parse(strDate);
    

    【讨论】:

      【解决方案3】:

      又快又脏:(+这是一个单线)

      String timeStr = "10:40 PM";
      
      int time = (Integer.parseInt(timeStr.substring(0, timeStr.indexOf(":"))) + (timeStr.endsWith("AM") ? 0 : 12) ) * 60 * 60 + 60 * Integer.parseInt(timeStr.substring(timeStr.indexOf(":") + 1, timeStr.indexOf(" ")));
      

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

      • 找出是AM 还是PM
      • 找出该语句的小时和分钟(substringindexOf 会有所帮助)
      • 如果是下午,则加 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(" ")));
      

      【讨论】:

      • @PierGiorgioMisley 是的,但我在这个例子中没有使用任何时间格式化程序
      • 当然,但它的解决方案更长更慢;)
      • 1.我不认为这是一个“更长”的解决方案...... 2.我提供的解决方案比使用日历的解决方案快大约 30 倍
      • 只是我的意见..你有一个解析,一个 sumstring,一个 indexof,加上一个 endwith,加上一个 if,再加上另一个 indexof 的另一个子字符串的另一个解析加上另一个 indexof。我们认为它比new SimpleDateFormat("HH:mm aa") 更令人困惑?
      • 这里不是讨论的地方...如果你愿意,我们可以在聊天中这样做,但是:当然,当你做一个单行时,它看起来很混乱,拆分它起来就好了。这样做会更快
      猜你喜欢
      • 2016-02-25
      • 1970-01-01
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2011-12-29
      相关资源
      最近更新 更多