Date does not receive value more 30 days

Asked

Viewed 54 times

3

I have a code that I want to check if the date is over 30 days with Jodatime, but putting in the console output the value is not assigned. I saw the following: https://stackoverflow.com/questions/16461361/add-one-day-into-joda-time-datetime

and I had the same idea, but it still didn’t work.

Follows the code :

DateTime dataEnvioPrevistoMaisTrintaDias = new DateTime();
dataEnvioPrevistoMaisTrintaDias = peg.getDataEnvioPrevisto().plus(30);
System.out.println("Data Envio : "+ peg.getDataEnvioPrevisto());
System.out.println("Data Envio + 30 : "+dataEnvioPrevistoMaisTrintaDias);

Console output:

Date Sent: 2017-09-29T00:00:00.000-03:00

Date Shipping + 30 : 2017-09-29T00:00:00.030-03:00

3 answers

4

You are using the wrong method. See the documentation for method plus(long):

Parameters:
duration - the Duration, in Millis, to add to this one

Translating:

Parameters:
duration - the duration, in Millis, to add to this object

That is, instead of adding up 30 days, you added up 30 milliseconds!

The method you should use is the plusDays(int).

4


I think the right thing would be plusDays() and not plus() as you are using. Change this line:

dataEnvioPrevistoMaisTrintaDias = peg.getDataEnvioPrevisto().plus(30);

for:

dataEnvioPrevistoMaisTrintaDias = peg.getDataEnvioPrevisto().plusDays(30);

I advise to give a read in this post teaching to manipulate classes in the new package java.time. since java-8, all the features of jodaTime have been natively added to the language.

3

The method you are using to add days, the plus(), is not the correct one. This one is for adding milliseconds, as the documentation indicates:

public Datetime plus(long Duration)

...

Parameters:

Duration - the Duration, in Millis, to add to this one

Instead you have to use the plusDays() adding the indicated quantity in days:

dataEnvioPrevistoMaisTrintaDias = peg.getDataEnvioPrevisto().plusDays(30);

Documentation for plusDays()

Browser other questions tagged

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