I am newbie in Android. I would like to use Calendar and set start and end date. Problem is, that i know only start date(today's date),but the end date is dynamically and is based on number of days. So for example: i've got int= 400 days and the end date should be something like that: start date + 400. How can i do it?
P.S Ideally would be if start and end date will be range.
Consider using java.time, the modern Java date and time API, for your date work.
ZoneId here = ZoneId.of("Europe/Tallinn");
int numberOfDays = 400;
LocalDate startDate = LocalDate.now(here);
LocalDate endDate = startDate.plusDays(numberOfDays);
System.out.format("From %s to %s%n", startDate, endDate);
When I ran this snippet just now, the output was:
From 2020-09-02 to 2021-10-07
The Calendar
class you asked about is poorly designed and long outdated. While it can do the job, it is cumbersome to work with, and for typical applications you will need conversions to and from Date
or other classes.
java.time is much nicer to work with.
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).