gpt4 book ai didi

java - 比较 Java 错误中的两个字符串时间

转载 作者:行者123 更新时间:2023-12-02 06:24:18 24 4
gpt4 key购买 nike

我有一个错误,因为我的应用程序在我比较 2 个字符串小时(time、time2)时停止,第一个是从我的时间选择器的结果中获取的,另一个是小米智能手机上的时间

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

第一种方式:

          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 和各种日志记录方法,以及通过添加断点来使用调试工具(它们可以更有效地查找应用程序中的错误)

关于java - 比较 Java 错误中的两个字符串时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55803887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com