0

I want add 2 days from today (29/9/2018), i use:

val calendar = Calendar.getInstance()!!
calendar.set(CalendarDay.today().year, CalendarDay.today().month+1, CalendarDay.today().day)
calendar.add(Calendar.DAY_OF_MONTH, 2)

But when i Log() it, date isn't 1/10/2018, it's 31/9/2018

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mducc
  • 743
  • 1
  • 9
  • 32
  • 1
    `Calendar.getInstance()` returns an instance with the current date/time already set. The second line in your snippet is unnecessary, and is likely where the issue is, since adding 1 to the month is not correct, in that case; i.e., `CalendarDay.today().month+1` is not what you want there. – Mike M. Sep 29 '18 at 04:43
  • I don't know, because CalendarDay.today().month start from 0, I do +1 for it – mducc Sep 29 '18 at 04:47
  • `Calendar` works with zero-based months, which means its `set()` method expects a zero-based month, as well. You would only ever add 1 if you need a "human-readable" month number. – Mike M. Sep 29 '18 at 04:49
  • The 1 day difference might be a timezone issue, e.g. when you Log the date in UTC and you're east of London and it's morning for you, it could can still be the day before for the date in UTC. – zapl Sep 29 '18 at 09:56
  • Possible duplicate of [Why is January month 0 in Java Calendar?](https://stackoverflow.com/questions/344380/why-is-january-month-0-in-java-calendar) – Ole V.V. Sep 29 '18 at 14:42

1 Answers1

3

For the older API you have chosen to use, you can simplify this to:

val today = Calendar.getInstance()                  // 2018-09-29
today.add(Calendar.DAY_OF_MONTH, 2);                // 2018-10-01

println(SimpleDateFormat().format(today.getTime())) // 2018-10-01

There is no need to set the Calendar instance which already contains the current date.

Your output I think maybe you misread or there is some odd bug in Android implementation because this code:

val today = Calendar.getInstance()                   // 2018-09-29

// totally unneccessary:
today.set(today.get(Calendar.YEAR),  
          today.get(Calendar.MONTH), 
          today.get(Calendar.DAY_OF_MONTH))          // 2018-09-29

today.add(Calendar.DAY_OF_MONTH, 2);                 // 2018-10-01

println(SimpleDateFormat().format(today.getTime()))  // 2018-10-01

Works fine although has the unnecessary step (setting it to be the date it already is). And if you add one to the month as you did before you would create the wrong date:

val today = Calendar.getInstance()                  // 2018-09-29

// unnecessary and wrong:
today.set(today.get(Calendar.YEAR), 
          today.get(Calendar.MONTH)+1, 
          today.get(Calendar.DAY_OF_MONTH))         // 2018-10-29

today.add(Calendar.DAY_OF_MONTH, 2);                // 2018-10-31

println(SimpleDateFormat().format(today.getTime())) // 2018-10-31

If you can use the newer JSR 310 API's that are available on newer Android, then it is better and that solution would be (assuming you wanted to use LocalDate):

val today = LocalDate.now()                           // 2018-09-29
val inTwoDays = today.plusDays(2)                     // 2018-10-01

println(DateTimeFormatter.ISO_DATE.format(inTwoDays)) // 2018-10-01

Please read about the java.time package for more classes that work with dates, calendars, time zones and more.

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • Android has support for `java.time` only as of API level 26, unless there's some Kotlin magic I'm not aware of. For previous versions, a library would be necessary, I believe. – Mike M. Sep 29 '18 at 05:05
  • 1
    @MikeM. I added older API version – Jayson Minard Sep 29 '18 at 05:12
  • You can use JSR-310, the modern Java date and time API, also on older Android versions. There is a backport. And you should certainly give it a thought since `Calendar` and friends have numerous design problems and the modern API is so much nicer to work with. How? See [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project) with a very through explanation. – Ole V.V. Sep 29 '18 at 14:46