How can I set the maximum date of a calendar using setMaxDate if I have the date with the format dd/mm/yyy?
I'm developing for Android.
How can I set the maximum date of a calendar using setMaxDate if I have the date with the format dd/mm/yyy?
I'm developing for Android.
I am assuming that you mean android.widget.DatePicker.setMaxDate.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("d/M/u");
String maxDateString = "21/12/2019";
long maxDateInMillis = LocalDate.parse(maxDateString, dateFormatter)
.atStartOfDay(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
yourDatePicker.setMaxDate(maxDateInMillis);
In my time zone the example date used above, 21/12/2019, produces a maxDateInMillis of 1 576 882 800 000. According to Epoch Converter (link at the bottom) this means
Your time zone: lørdag d. 21. december 2019 kl. 00:00:00 GMT+01:00
According to the linked documentation DatePicker uses the device time zone.
Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.
org.threeten.bp with subpackages.DatePicker.setMaxDate documentationjava.time was first described.java.time to Java 6 and 7 (ThreeTen for JSR-310).