How to check if it's already another day?

I am making an application where a feature becomes available until a condition is reached, after that I have to disable the feature and only enable it the other day.

How do I know it's another day?

 3
Author: Éowyn, 2016-06-24

1 answers

You can check in several ways, such as:

24h in milliseconds => 24 (H) * 60 (Minutes) * 60 (seconds) * 1000 (milliseconds) so you can just check if the difference between dates in milliseconds is greater than 86,400,000.

Or add the following code to check:

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 better understanding I advise you to take a look at the official Oracle documentation on the subject, here,here and here .

You can also use Joda-Time which according to their documentation:

The normal date and time in previous versions for Java SE 8 are poor. By addressing this issue head-on, Joda-Time became in fact the default for date and time over the Java library before Java SE 8. Note that from Java SE 8 onwards, users are invited to migrate to java.time (JSR-310) - a essential part of JDK that replaces this project.

But after all why use Joda-Time ?

Productivity will greatly increase with the use of a library that provides you with simple results for processes that were previously 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.

Has extensive community and documentation to assist you in questions or problems that may occur.

Time Zone calculations are updated several times a year to ensure data consistency. These are updated from http://www.iana.org/time-zones

Has simpler methods than the Calendar class.

Is Open-source, meaning anyone can study your source code and contribute if they feel relevant.

To power use Joda-Time, do the following:

1st download at this link the Joda-Time library.

2nd to use Joda-Time in your project just add the jar of the Downloads section in your project's classpath.

3rd to import 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 because it is already part of java, through the package java.time.

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 this link so you can see the Joda-Time timezone list in case you want to change.

 5
Author: Falion, 2020-06-11 14:45:34