Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:17:49+00:00 2026-06-14T09:17:49+00:00

Possible Duplicate: If statement using == gives unexpected result I am working on a

  • 0

Possible Duplicate:
If statement using == gives unexpected result

I am working on a java code that is suppose to do something if the time is equal you set in the code and the real time in the system.And if the time doesn’t match it completely ignores it. Now I can got the time correctly but for some reason the if statement is saying its not equal even when it is. Is there something I am missing or doing wrong?

Calendar now = new GregorianCalendar();
String am_pm;     
int hour = now.get(Calendar.HOUR);
int minute = now.get(Calendar.MINUTE);
if (now.get(Calendar.AM_PM) == 0){
    am_pm = "AM";  
}
else{
    am_pm = "PM";
}
String time = hour + ":" + minute + " " + am_pm;        
String Scan_hour = "2:45 PM";       
System.out.println(time + "--" + Scan_hour);    
if (time == Scan_hour){
    System.out.println("Time matchs");
}
else{
    System.out.println("Time Doesn't Match");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:17:51+00:00Added an answer on June 14, 2026 at 9:17 am
    if (time == Scan_hour){
    

    should be

    if (time.equals(Scan_hour)){
    

    String comparison should use equals() instead of == (except the case of String literals).

    == checks for reference equality (both references points to same object or not). equals() checks for content equality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why is using a wild card with a Java import statement bad?
Possible Duplicate: Howto build a SQL statement with using IDs that might not be
Possible Duplicate: Why is using a wild card with a Java import statement bad?
Possible Duplicate: Java String.equals versus == I am using an if statement in java
Possible Duplicate: Using a bitwise & inside an if statement I have the following
Possible Duplicate: Why is my comparing if statement not working? I am attempting to
Possible Duplicate: ‘break’ statement when using curly braces in switch-case While merging a package
Possible Duplicate: Java String.equals versus == I am using jcreator to practice java language.
Possible Duplicate: get jquery to return font-size in points Using jQuery, I'm setting something
Possible Duplicate: Response.Redirect(“”) inside “using{ }” Pretty much that. If I had, for example,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.