分配时间与系统时间相同时运行方法

时间:2016-12-24 02:54:57

标签: java android

当我的指定时间与系统时间相同时,我正在尝试运行我的方法。我确信两个日历都打印出相同的结果,但它不会运行该方法。

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();
    }

}

}

感谢你

1 个答案:

答案 0 :(得分:1)

比较字符串时,请使用.equals函数,而不是==:

更改

if (aa == bb){

if (aa.equals(bb)){

以下是有关此内容的更多信息:What is the difference between == vs equals() in Java?