Mohsen Mirshekari Mohsen Mirshekari - 1 year ago 84
Android Question

Operator '<' cannot be applied to 'java.lang.String'

i got the system time in a string for example something like "1240".
then i wanted to do something like if the system time was < than 1240,then close the application.
but it gives me the "Operator '<' cannot be applied to java.lang.String!" Error!

My code is :

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

What's the problem?

Answer Source

< is not defined for a string and an int of course . So you can't use it .

your current time can be calculated like this :

int curTime = 100*hours + minutes;

then you can use < between two integers.

I believe though you must use System Milliseconds which is more usual.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download