Comparing two TIME variables using java
I want to compare the system time with manually given time, th开发者_开发知识库is 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.
This gives the hour (0-23):
int hour = calendar.get(Calendar.HOUR_OF_DAY);
in Java, you compare strings using string1.equals(string2), not using ==
Strings and other objects should be compared with .equals() method, not == opertor
精彩评论