运营商''不能应用于' java.lang.String'

时间:2016-11-17 20:59:03

标签: java android

我在一个字符串中得到了系统时间,例如" 1240"。 然后我想做一些事情,如果系统时间是<比1240,然后关闭应用程序。 但它给了我"运营商''不能应用于java.lang.String!"错误!

我的代码是:

            runOnUiThread(new Runnable() {
            public void run() {
                try{
                    TextView txtCurrentTime= (TextView)findViewById(R.id.showtime);
                    Date dt = new Date();
                    int hours = dt.getHours();
                    int minutes = dt.getMinutes();
                    int mynum = 1240;
                    String curTime = hours + "" + minutes;
                    txtCurrentTime.setText(curTime);
                    if(curTime < mynum ){
                        System.exit(0);
                    }
                }catch (Exception e) {}
            }
        });

问题是什么?

3 个答案:

答案 0 :(得分:1)

&LT;当然没有为字符串和int定义。所以你不能使用它。

您当前的时间可以这样计算:

int curTime = 100*hours + minutes;

那么你可以使用&lt;两个整数之间。

我相信你必须使用更常见的系统毫秒数。

答案 1 :(得分:1)

尝试{

 SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
 String str1 = String.valueOf(hours1) + ":" + String.valueOf(minutes1) + ":" + "00";
 String str2 = String.valueOf(hours2) + ":" + String.valueOf(minutes2) + ":" + "00";
 Date date1 = formatter.parse(str1);
 Date date2 = formatter.parse(str2);

 if (date1.compareTo(date2)<0)
  {
    //       if date2 > date1                
  }

}catch (ParseException e1){
    e1.printStackTrace();
}

formats for dates

根据您的要求检查日期/时间格式

答案 2 :(得分:0)

if(hours * 100 + minutes < mynum){
    System.exit(0);
}