1

I am trying to run my method when my assigned time is the same as system time. I am sure that both calendar printout the same result but it doesn't run the method.

public class Intake extends Fragment {

TextView tv1, tv2,tv3;

public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState){

    return inflater.inflate(R.layout.intake, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    tv1 = (TextView) getActivity().findViewById(R.id.tv1);
    tv2 = (TextView) getActivity().findViewById(R.id.tv2);
    tv3 = (TextView) getActivity().findViewById(R.id.tv3);

    Calendar settime = Calendar.getInstance();
    settime.set(Calendar.HOUR_OF_DAY, 10);
    settime.set(Calendar.MINUTE,46);
    settime.set(Calendar.SECOND,10);

    Calendar systime = Calendar.getInstance();
    systime.getTime();


    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");


    String aa = String.valueOf((sdf.format(settime.getTime())));
    String bb = String.valueOf((sdf.format(systime.getTime())));

    tv1.setText(aa);
    tv2.setText(bb);
    if (aa == bb){
       runMath();
    }

}

}

THank you

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
kaikai
  • 71
  • 8
  • 1
    Your issue might be that you're only checking when your view is created, and not continuously. Also with something like time you can't expect exact matches, and should compare with a threshold or greater/less than. – cameronlund4 Dec 24 '16 at 03:09

1 Answers1

1

When comparing Strings, use the .equals function, not ==:

Change

if (aa == bb){

to

if (aa.equals(bb)){

Here's some more information on this: What is the difference between == vs equals() in Java?

Community
  • 1
  • 1
Abdulgood89
  • 359
  • 2
  • 9
  • Thank you! But @StaticShadow is right, it will only check once when view is created not continuously. – kaikai Dec 24 '16 at 03:51