I'm new to Android and I'm trying to get the current date but the Android Calendar
class requires API 24. I need to support older device and the Time
class is deprecated since API 22.
The problem is how to get current date on API 23 and I solved it by using java.util.Calendar
which works on all versions. So what should I use, Android calendar or Java calendar?
Note that day, month and year are just integers when using the Android calendar.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
day =android.icu.util.Calendar.getInstance().get(android.icu.util.Calendar.DAY_OF_MONTH);
month = android.icu.util.Calendar.getInstance().get(android.icu.util.Calendar.MONTH);
year = android.icu.util.Calendar.getInstance().get(android.icu.util.Calendar.YEAR);
}
else {
day = java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
}
And when using only Java calendar there's no need to check the API version
day = java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);