比较Java Errorr中的两个字符串小时

时间:2019-04-23 03:26:46

标签: java android simpledateformat java-time

我遇到一个错误,导致我在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);
                }

1 个答案:

答案 0 :(得分:0)

结合@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的各种日志记录方法以及调试工具(它们更有效地发现应用程序中的错误)