How do you make sure it’s another day?

Asked

Viewed 300 times

3

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

How do I know it’s another day?

  • What version of Java are you using?

  • What time of day you need to make available?

  • In fact it is all day, the functionality will be disabled qnd a condition is reached. The version is 1.7

1 answer

5


You can check in several ways, such as :

24h in Milliseconds => 24 (h) * 60 (minutes) * 60 (seconds) * 1000 (Milliseconds) so you can only verify that 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 that according to their documentation :

The normal date and time in previous versions for Java SE 8 is poor. By addressing this problem head-on, Joda-Time has in fact become the standard for date and time on the Java library before Java SE 8. Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - an essential part of the JDK replacing this project.

But after all why use Joda-Time ?

Productivity will increase greatly with the use of a library that provides simple results for processes that were previously considered complex.

It provides better performance than the Calendar class, usually used for date manipulation purposes. This occurs due to the minimum calculation performed in 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 consistency of the data. These are updated from http://www.iana.org/time-zones

Has simpler methods than the Calendar class.

It’s open-source, which means anyone can study your source code and contribute if they find it relevant.

In order to use Joda-Time, :

1 º Download at this link the Joda-Time library.

2º To use Joda-Time in your project just add the download section jar in the classpath of your project.

3º To import Joda-Time classes use the following reference :

import org.joda.time.classeQueVoceQuerImportar

OBS : If you are using version 1.8 of jdk, you do not need to import the lib from Joda-Time because it is already part of java, through the java.time package..

Using the 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 time time zone list in case you want to change.

  • It would be interesting to explain what this code (from java.time) is doing, if the OP does not know how to work.

  • I would like to use the 2 option but I do not understand mt well how it works. Every time I open the application, I call the isYesterday function by passing the current date as parameter?

  • @diegofm Yes it’s interesting,.

  • @Amandafagundes Yes you pass the current date as parameter.

  • @Falion I referred to the mode with jodatime, which is more recommended for this, however, it is more complicated to understand if you are not used to.

  • @diegofm Ok, I will also put a more detailed answer on the subject.

  • If you are using version 1.8 of jdk, you do not need to import this lib from jodatime, as it is already part of java, via the java.time package.

  • @diegofm Thanks for the info,I will put her also in the reply.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.