Date does not receive value more than 30 days

3

I'm having a code that I want to check if the date is greater than 30 days with the JodaTime, but putting in the console output the value is not assigned. I have seen the following: link

And I did the same idea, but it still did not work.

Follow 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 Submitted: 2017-09-29T00: 00: 00.000-03: 00

     

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

    
asked by anonymous 29.09.2017 / 17:19

3 answers

4

I think the correct one would be plusDays() and not plus() like you're using. Change this line:

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

To:

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

I'd advise reading in this post that teaches you to handle classes in the new java.time package. because as of java-8, all the functionality of jodaTime was added natively to the language.

    
29.09.2017 / 17:31
4

You are using the wrong method. See the 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 30 days, you added 30 milliseconds!

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

    
29.09.2017 / 17:31
3

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

  

public DateTime plus (long duration)

     

...

     

Parameters:

     

duration - the duration, in millis, to add to this one

Instead you have to use plusDays() which adds the indicated amount in days:

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

Documentation for plusDays()

    
29.09.2017 / 17:32