tl;dr
Period.between(
LocalDate.now() ,
LocalDate.of(
myDatePicker.getYear() ,
myDatePicker.getMonth() ,
myDatePicker.getDayOfMonth()
)
)
Details
You Question is not clear, but it seems you are trying to calculate the elapsed time between the chosen date and the current date (today).
Avoid legacy date-time classes
Do no use the troublesome legacy date-time classes such as java.util.Date
and java.util.Calendar
. These are now legacy, supplanted by the java.time classes. Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
Also, Date
and Calendar
represent a date with a time-of-day. You need a date-only for your purposes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
Time zone
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
You can either use the current default time zone, or, if important, ask the user for their intended/expected time zone.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.systemDefault(); // or ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
Do not bother with constructing strings to build a date. Simply ask the DatePicker
object for its data.
int year = myDatePicker.getYear() ;
int month = myDatePicker.getMonth() ;
int dayOfMonth = myDatePicker.getDayOfMonth() ;
Make a LocalDate
from those.
LocalDate ld = LocalDate.of( year , month , dayOfMonth ) ;
Period
To represent the span of time between the chosen date and today, use the Period
class.
Period period = Period.between( today , ld );
Generating String
as output
To see that value as a string in standard ISO 8601 format, call toString
. You will result such a PT3D
for three days. If your users can be trained to understand this compact format, consider using it.
To build your own string as output, interrogate for the number of days, months, and years in the Period
object.
int days = period.getDays();
int days = period.getMonths();
int years = period.getYears();
String output = "years: " + years + "months: " + months + "days: " + days ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android