tr_tech
tr_tech

Reputation: 33

Comparing two TIME variables using java

I want to compare the system time with manually given time, this program is not satisfying the condition. How can i do this in 24hrs format.

    Calendar calendar = new GregorianCalendar();  
    String am_pm;  
    int hour = calendar.get(Calendar.HOUR);  
    int minute = calendar.get(Calendar.MINUTE);  
    int second = calendar.get(Calendar.SECOND);  
    if(calendar.get(Calendar.AM_PM) == 0)  
      am_pm = "AM";  
    else  
      am_pm = "PM";    
    String time = hour + ":" + minute + ":" + second + " " + am_pm ;  
    String time1 =  12 + ":" + 40 + ":" + 00 + " " + "PM";  
    if(time==time1)    
    {     
        display(time);  
    }  
    else   
    {  
        display(time);  
    }

I have to do it in android Application. So pls help me.

Upvotes: 0

Views: 1108

Answers (3)

Papuass
Papuass

Reputation: 327

Strings and other objects should be compared with .equals() method, not == opertor

Upvotes: 0

jcomeau_ictx
jcomeau_ictx

Reputation: 38492

in Java, you compare strings using string1.equals(string2), not using ==

Upvotes: 0

Maurice Perry
Maurice Perry

Reputation: 32831

This gives the hour (0-23):

int hour = calendar.get(Calendar.HOUR_OF_DAY);

Upvotes: 5

Related Questions