I'm making an application where a feature is available until a condition is reached, after that I have to disable the functionality and just enable it the other day.
How do I know it's another day?
I'm making an application where a feature is available until a condition is reached, after that I have to disable the functionality and just enable it the other day.
How do I know it's another day?
You can check in a number of ways, such as:
24h in Milliseconds = > 24 (h) * 60 (minutes) * 60 (seconds) * 1000 (Milliseconds) so that you can only check if the difference between dates in milliseconds is greater than 86,400,000.
Or add the following code to verify:
public static final long ONE_MINUTE = 60 * 1000; // Criando uma variável static para dizer que um minuto em milissegundos equivale a 60.000;
public static final long ONE_HOUR = 60 * ONE_MINUTE; // Criando uma variável static para dizer que uma hora em milissegundos equivale a 3.600.000;
public static final long ONE_DAY = 24 * ONE_HOUR;// Criando uma variável static para dizer que 24 horas em milissegundos equivale a 86.400.000;
public static boolean isYesterday(Date d)//Criando uma classe Date chamada d {
return DateUtils.isToday(d.getTime() + ONE_DAY);//O método getTime() retorna o número de milissegundos desde 1 de janeiro de 1970, 00:00:00 GMT representado por este objeto Date.
}
For a better understanding, I urge you to take a look at the official Oracle documentation on the subject, here , here and here .
You can also use Joda-Time according to their documentation:
The normal date and time in earlier versions for Java SE 8 are poor. In addressing this problem head-on, Joda-Time has in fact become the default for date and time on the library for Java before Java SE 8. Note that from Java SE 8 onwards, users are invited to migrate to java.time (JSR-310) - an essential part of the JDK that replaces this project.
But why use Joda-Time?
Productivity will increase dramatically with the use of a library that provides simple results for processes that were once considered complex.
Provides better performance than the Calendar class, usually used for date manipulation purposes. This is due to the minimum calculation performed on the access of some field.
It has a large community and documentation to assist you with any questions or problems that may arise.
Time Zone calculations are updated several times a year to ensure data consistency. These are updated from link
It has simpler methods than the Calendar class.
It is Open-source, which means that anyone can study their source code and contribute if they find it relevant.
In order to use Joda-Time, do the following:
1st Download in this link the Joda-Time library.
2º To use Joda-Time in your project simply add the jar of the downloads section in the classpath of your project.
3rd To import the Joda-Time classes use the following reference:
import org.joda.time.classeQueVoceQuerImportar
NOTE: If you are using version 1.8 of jdk, you do not need to import the Joda-Time lib since it is already part of java, through the java.time package.
Using Joda-Time:
Add the following code when using Joda-Time:
DateTimeZone timeZone = DateTimeZone.forID( "America/Sao_Paulo" );
DateTime dateTimeInQuestion = new DateTime( 2016, 1, 2, 3, 4, 5, 6, timeZone ); // Or: new DateTime( someJavaDotUtilDotDateObject );
DateTime now = new DateTime( timeZone );
DateTime twentyFourHoursFromNow = now.plusHours( 24 ); // Ignores Daylight Saving Time (DST). If you want to adjust for that, call: plusDays( 1 ) instead.
DateTime isDateTimeInQuestionAfter24HoursFromNow = dateTime.isAfter( twentyFourHoursFromNow );
Take a look at in this link so you can see the Joda-Time time zone list if you want change.