比较 Java 错误中的两个字符串小时数

Comparing two String hours in Java Errorr

我有一个错误导致我的应用程序在我比较 2 个字符串小时(时间,time2)时停止,第一个是我的时间选择器的结果,另一个是 mi 智能手机上的时间

我尝试比较购买的字符串的失败方法:

第一种方式:

          if(time.equals(time2))
          {

             Toast.makeText(getApplicationContext(), "SAME VALUE: "+time, Toast.LENGTH_LONG).show();
          }
          else
          {

          }

第二种方式:

          if(time == time2)
          {

             Toast.makeText(getApplicationContext(), "SAME VALUE: "+time, Toast.LENGTH_LONG).show();
          }
          else
          {

          }

这是所有代码

                 @Override
                public void onTimeSet(TimePicker view, int hour, int minute) {

                    TextView textView = (TextView)findViewById(R.id.timetext);
                    textView.setText("Hour: " + hour + "   Minutes:" + minute );

                    String Shour = Integer.toString(hour);
                    String Sminute = Integer.toString(minute);
                    final String time2 = Shour + ":" +Sminute;
                    Calendar calendar = Calendar.getInstance();
                    SimpleDateFormat format = new SimpleDateFormat("HH:mm");

                    final String time = format.format(calendar.getTime());
                    Toast.makeText(getApplicationContext(), "vlaue is "+time, Toast.LENGTH_LONG).show();

                    Timer t = new Timer();
                    t.scheduleAtFixedRate(new TimerTask() {
                        @Override
                        public void run() {
                            if(time.equals(time2)){
                                Toast.makeText(getApplicationContext(), "SAME VALUE: "+time, Toast.LENGTH_LONG).show();
                            }
                            else{

                            }
                        }
                    },0,1000);
                }

结合@racraman在关于timer2的评论中提到的内容和上面的答案。这是一个答案

     ...
     // format the string of timer2 to add a zero when it is a single digit so that it matches with the date format you have used ie HH:mm
      @Override
            public void onTimeSet(TimePicker view, int hour, int minute) {

                TextView textView = (TextView)findViewById(R.id.timetext);
                textView.setText("Hour: " + hour + "   Minutes:" + minute );

                // String Shour = Integer.toString(hour);
                // String Sminute = Integer.toString(minute);
                final String time2 = String.format(Locale.getDefault(),"%02d:%02d", hour, minute);
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat format = new SimpleDateFormat("HH:mm");

                final String time = format.format(calendar.getTime());
                Toast.makeText(getApplicationContext(), "vlaue is "+time, Toast.LENGTH_LONG).show();

                Timer t = new Timer();
                t.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        if(time.equals(time2)){
                            // Toast.makeText(getApplicationContext(), "SAME VALUE: "+time, Toast.LENGTH_LONG).show();
                            runOnUiThread(new Runnable() {
                              public void run() {
                                 Toast.makeText(getApplicationContext(), "SAME VALUE: "+time, Toast.LENGTH_LONG).show();
                              }
                            }
                        }
                        else{

                        }
                    }
                },0,1000);
            }

此外,随着您的进步,学习通过各种日志记录方法充分利用 Logcat 以及通过添加断点的调试工具(它们更有效地发现您应用中的错误)