java.time
LocalTime now = LocalTime.now(ZoneId.systemDefault());
if (now.isAfter(LocalTime.of(9, 0))) {
System.out.println("It’s past 9");
}
Notice how much clearer it reads than the code in your question.
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, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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).
What went wrong in your code?
You need a return statement in your method. Assuming you wanted to return time
, the method produces a string like 2018/08/28 09:21:03
. The hours have been set to 9, but the minutes and seconds are unchanged from the current time. Also you are producing a similar string for current system time, but not comparing the two.
Links