88

I have date Wed May 08 00:00:00 GMT+06:30 2013. I add one day into it by using Joda-Time DateTime like this.

DateTime dateTime = new DateTime(date);
dateTime.plusDays(1);

When I print dateTime, I got this date 2013-05-08T00:00:00.000+06:30. The joda date time didn't add one day. I haven't found any error.

Thanks

JodaStephen
  • 60,927
  • 15
  • 95
  • 117
user1156041
  • 2,155
  • 5
  • 24
  • 54
  • 9
    I don't know joda, but I assume `plusDays()` returns a new `DateTime` object. Try `datetime = dateTime.plusDays(1)`. Confirmed from the [docs](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#plusDays%28int%29). – Phylogenesis May 09 '13 at 12:07
  • @Phylogenesis you might want to post that as an answer, because it is correct. – Barend May 09 '13 at 12:08
  • 1
    @Barend it's been posted as an answer now. No need to clutter the question. – Phylogenesis May 09 '13 at 12:10
  • 1
    I was apparently in the process of posting the answer when the comment thread happened. Had I seen it happening, I would have agreed Phylogenesis should post as an answer. – Don Roby May 09 '13 at 12:39

2 Answers2

178

The plusDays method is not a mutator. It returns a copy of the given DateTime object with the change made rather than changing the given object.

If you want to actually change the variable dateTime value, you'll need:

DateTime dateTime = new DateTime(date);
dateTime = dateTime.plusDays(1);
Don Roby
  • 40,677
  • 6
  • 91
  • 113
32

If you want add days to current date time instance, use MutableDateTime

MutableDateTime dateTime = new MutableDateTime(date);  
dateTime.addDays(1);
JodaStephen
  • 60,927
  • 15
  • 95
  • 117
Ilya
  • 29,135
  • 19
  • 110
  • 158