I have mostly spent 1 day for just comparing two times but no worth result until now. I am basically trying to compare between two different times which I want to avoid storing data id the first time less than the second time but I couldn't. In addition, the equal part is working smoothly but the second part not working
@Override
public void onTimeSet(int id, TimePicker view, int hourOfDay, int minute) throws ParseException {
String AM_PM = " AM";
String pattern = "HH:mm";
SimpleDateFormat format = new SimpleDateFormat(pattern);
String mm_precede = "";
if (hourOfDay >= 12) {
AM_PM = " PM";
if (hourOfDay >=13 && hourOfDay < 24) {
hourOfDay -= 12;
}
else {
hourOfDay = 12;
}
} else if (hourOfDay == 0) {
hourOfDay = 12;
}
if (minute < 10) {
mm_precede = "0";
}
if (id == 1 && id ==2) {
Date date1 = format.parse(sTimeApp.getText().toString());
Date date2 = format.parse(eTimeApp.getText().toString());
DateTime da1 = new DateTime(date1);
DateTime da2 = new DateTime(date2);
System.out.println("Date" + date1);
System.out.println("Date" + date1);
if (da1.equals(da2) ){
Toast.makeText(this, "Error , Time should be different ", Toast.LENGTH_SHORT).show();
return;
}else if (da1.isBefore(da2)){
Toast.makeText(this, "Error , Time should be different ", Toast.LENGTH_SHORT).show();
return;
}
}
// if (sTimeApp.equals(eTimeApp) ) {
// Toast.makeText(this, "Error , Time should be different ", Toast.LENGTH_SHORT).show();
// return;
// }
if (id == 1) {
if (minute <= 9) {
sTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
} else {
sTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
}
} else if (id == 2) {
if (minute <= 9) {
eTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
} else {
eTimeApp.setText(hourOfDay + ":" + mm_precede + minute + AM_PM);
}
}
}
}
java.time
I think you’re after something like this:
If you are already using Joda-Time, you may not want to upgrade to java.time just now. Joda-Time too has a
LocalTime
class that you can use much in the same way as I am usingorg.threeten.bp.LocalTime
in the above.I am unsure whether you also wanted to compare the time set through the time picker to either the start or end time already in
sTimeApp
oreTimeApp
. I have not introduced such a comparison.What went wrong in your code?
id == 1 && id ==2
cannot be true. In my code I have suggestedid == 1 || id == 2
, but please check what you want.DateTimeFormatter
for formatting the time (again, Joda-Time has similar functionality).SimpleDateFormat
andDate
classes. Instead I recommend you use java.time, the modern Java date and time API. If you are already using Joda-Time, that’s a nice option too. In both cases avoid the mentioned outdated classes completely since either Joda-Time or java.time offers all the functionality you need.Question: Can I use java.time on Android?
Yes,
java.time
works nicely on older and newer Android devices. It just requires at least Java 6.org.threeten.bp
with subpackages.Links
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).LocalTime
documentation