I am using this code:
DateTime(date)//Thu Aug 06 00:00:00 GMT+03:00 2020
.plusDays(days) // 103
.toDate()
And the result is Fri Dec 18 23:00:00 GMT+02:00 2020
instead of Dec 19
.
With some dates it work well, with other the result date-1, I guess the problem is with number of days in month, but does plusDays()
not consider it?
It does consider it. The problem with the two date-time strings you have mentioned is that they belong to different Zone-Offset (the first one is with UTC+3 and the second one with UTC+2). Given below is how to do it with the same Zone-Offset.
Output:
I recommend you use the modern
java.time
date-time API and the corresponding formatting API (package,java.time.format
). Learn more about the modern date-time API from Trail: Date Time. If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.The following table shows an overview of modern date-time classes:
With modern date-time API:
Output:
Alternatively,
Output: