Calendar.getInstance().YEAR
is a constant that represents the year field (which has a value equals to 1
). Actually, it's the same as calling Calendar.YEAR
directly.
But this is not the year value of the Calendar
object. When you do set(java.util.Calendar.getInstance().YEAR
you're setting the year to 1
(which is probably not what you want). The same applies for MONTH
, MINUTE
and all other constants you're using.
Another detail is that you don't need to create another calendar (by calling Calendar.getInstance()
) all the time. That's redundant and not necessary in your code.
Anyway, first things first. In the first 2 lines, you're printing the MINUTE
constant, not the minutes value of the dates. If you want to print the full date (which is much more useful than just the minutes, IMO), just call the getTime()
method. Also, set the values to variables before using them, it makes the code much cleaner:
import java.util.Calendar;
Calendar lastTime = DataManager.getInstance().getCurrentUser().getLastTime();
Calendar now = Calendar.getInstance(); // current date/time
Log.e("Data Manager Time: ", lastTime.getTime() + "");
Log.e("Current Time: ", now.getTime() + "");
Just reminding that this will print the dates in the device's JVM default timezone. If you want to change the format, just check in this link.
If you still want to print just the minutes, change the call to getTime()
to get(Calendar.MINUTE)
.
Now the rest of your logic (if I understood correctly) will be like this:
if (lastTime.after(now)) {
lastTime = (Calendar) now.clone(); // lastTime becomes a copy of "now"
lastTime.add(Calendar.MINUTE, 30); // add 30 minutes
} else if (Utilities.isFirstTimePressed) {
Utilities.isFirstTimePressed = false;
lastTime.add(Calendar.MINUTE, 30); // add 30 minutes
} else {
Toast.makeText(mainclass, "Button is disabled for 30 minutes", Toast.LENGTH_SHORT).show();
}
Java new Date/Time API
The old classes (Date
, Calendar
and SimpleDateFormat
) have lots of problems and design issues, and they're being replaced by the new APIs.
In Android you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need the ThreeTenABP (more on how to use it here).
One way to check if 30 minutes has passed is to use the org.threeten.bp.Instant
class. Then you use the isAfter
method to compare, and a org.threeten.bp.temporal.ChronoUnit
to add 30 minutes.
I also use a org.threeten.bp.ZoneId
to convert the date to the JVM default timezone (just to print it). In this link there are also answers that cover the new API, using a DateTimeFormatter
, so you can refer to them if you want to change the format.
I also use the org.threeten.bp.DateTimeUtils
class to convert from and to java.util.Calendar
. The code will be like this:
// convert java.util.Calendar to org.threeten.bp.Instant
Instant lastTime = DateTimeUtils.toInstant(DataManager.getInstance().getCurrentUser().getLastTime());
Instant now = Instant.now();
Log.e("Data Manager Time: ", lastTime.atZone(ZoneId.systemDefault()) + "");
Log.e("Current Time: ", now.atZone(ZoneId.systemDefault()) + "");
if (lastTime.isAfter(now)) {
// add 30 minutes to current date/time
lastTime = now.plus(30, ChronoUnit.MINUTES);
} else if (Utilities.isFirstTimePressed) {
Utilities.isFirstTimePressed = false;
lastTime = lastTime.plus(30, ChronoUnit.MINUTES);
} else {
Toast.makeText(mainclass, "Button is disabled for 30 minutes", Toast.LENGTH_SHORT).show();
}
// convert back to java.util.Calendar (set it anywhere you need)
DateTimeUtils.toGregorianCalendar(lastTime.atZone(ZoneId.systemDefault()));
I'm using ZoneId.systemDefault()
, which takes the JVM default timezone (the same used when you create Calendar.getInstance()
, so it usually won't be much a problem, unless the user changes it).
The problem is that the default timezone can be changed, even at runtime, so the ideal is to specify one, if you possibly can. The API uses IANA timezones names (always in the format Region/City
, like America/Sao_Paulo
or Europe/Berlin
).
Avoid using the 3-letter abbreviations (like CST
or PST
) because they are ambiguous and not standard.
You can get a list of available timezones (and choose the one that fits best your system) by calling ZoneId.getAvailableZoneIds()
.