Jim
Jim

Reputation: 19552

How can I add 1 second in Joda date/time?

I have a LocalDateTime object myDateTime that I can see in the debugger that has:
2015-12-12T23:59:59.000
I do: myDateTime.plusSeconds(1) but the timestamp remains the same.
What am I messing up?

Upvotes: 2

Views: 1171

Answers (2)

jhd
jhd

Reputation: 11

plusSeconds() returns a copy of your current date time with the added seconds. So instead of doing myDateTime.plusSeconds(1) and using myDateTime, you would do the following: LocalDateTime newDateTime = myDateTime.plusSeconds(1)

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500105

Most types in Joda Time (at least the ones you should use) are immutable. You can't change their value - but you can call methods which return a new value. You're calling the right method in this case, but you need to remember the result, e.g.

myDateTime = myDateTime.plusSeconds(1);

Upvotes: 7

Related Questions