Comparing Two Times in Android

how to compare two times in android

Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat formatter1 = new SimpleDateFormat("dd/M/yyyy h:mm");
String currentDate = formatter1.format(calendar1.getTime());

final String dateString = cursor.getString(4);
final String timeString = cursor.getString(5);
String datadb =dateString+" "+timeString;

// Toast.makeText(context,"databse date:-"+datadb+"Current Date :-"+currentDate,Toast.LENGTH_LONG).show();

if(currentDate.compareTo(datadb)>=0) {
myCheckBox.setChecked(true);
myCheckBox.setEnabled(false);
}

Comparing two times in android

Change SimpleDateFormat like below...

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

Below are the patterns:

H   Hour in day (0-23)  Number  0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12

This will work....

How to Compare Two time(12 hours format) in android

java.time and ThreeTenABP

    DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);

String s1 = "11:04 AM";
String s2 = "1:00 PM";
LocalTime time1 = LocalTime.parse(s1, timeFormatter);
LocalTime time2 = LocalTime.parse(s2, timeFormatter);
if (time1.isBefore(time2)) {
System.out.println("time1 < time2");
} else {
System.out.println("time1 >= time2");
}

Output from this snippet is:

time1 < time2

The snippet shows how to parse your time strings into LocalTime objects and compare them using isBefore. There are also methods isEqual and isAfter. However, even better. store your times as time datatype in your SQL database and retrieve LocalTime objects directly from your database and avoid the parsing.

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.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

How can I compare two hours in java?

instead of

nowh.before(enh) && nowh.after(sth)

use

nowh.before(enh) && nowh.after(sth) && sth.before(enh)
|| enh.before(sth) && !(nowh.before(enh) && nowh.after(sth))

Apart from that I think Calendar class is supposed to be used differently I assume...

compare two times format HH:MM java-android

You can use compareTo . compareTo() method is defined in interface java.lang.Comparable .

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String getCurrentTime = sdf.format(c.getTime());
String getTestTime="09:45";

if (getCurrentTime .compareTo(getTestTime) < 0)

{
// Do your staff
Log.d("Return","getTestTime less than getCurrentTime ");
}
else
{
Log.d("Return","getTestTime older than getCurrentTime ");
}

How to compare Time in java/android (given input in strings)?

Well, if they're actually hh:mm (including leading zeroes, and in 24-hour format) then you can just compare them lexicographically (i.e. using String.compareTo(String)). That's the benefit of a sortable format :)

Of course, that won't check that both values are valid times. If you need to do that, you should probably parse both times: check the length, check the colon, parse two substrings, and probably multiply the number of hours by 60 and add it to the number of minutes to get a total number of minutes. Then you can compare those two totals.

EDIT: As mentioned in the comments, if you do need to parse the values for whatever reason, personally I would recommend using Joda Time (possibly a cut down version, given the mobile nature) rather than SimpleDateTimeFormat and Date. Joda Time is a much nicer date and time API than the built-in one.

Difference between two times in Android Studio, (Registered time and current time)

This answer lead me the right way, actually my approach was good I just needed to make a function that showed only the difference.

I made this one. Hope it helps somebody.

Just call the function with the result of you operation, at the end I used Date().getTime() instead of System.currentTimeMillis() but I think it should be the same.

The complete code:

startTime = System.currentTimeMillis().toDouble()

timer = Timer()
timer.schedule(timerTask {
runOnUiThread {
advanceTimer()
}
}, 0, 60)

fun advanceTimer() {
//Total time since timer started, in seconds
val currentTime = System.currentTimeMillis().toDouble()
time = currentTime-startTime

// Shows the time in a label on the screen
timerString.text = differenceResult(time)
}
fun differenceResult(time: Long): String {
var x: Long = time / 1000

var seconds = x % 60
x /= 60
var minutes = x % 60
x /= 60
var hours = (x % 24).toInt()
x /= 24
var days = x
return String.format("%02d:%02d:%02d", hours, minutes, seconds)
}


Related Topics



Leave a reply



Submit