I am receiving date string 2021-06-13T15:00:00.000Z from rest api call. I have to parse this date string that match will start in 5 hours or today
You can try something like below.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
LocalDateTime eventDate = LocalDateTime.parse("2021-06-15T21:00:00.000Z", formatter);
LocalDateTime now = LocalDateTime.now();
Duration dur = Duration.between(now, eventDate);
if (dur.isNegative()) {
Log.d("DINKAR", "Time Already Passed");
} else if (dur.isZero()) {
Log.d("DINKAR", "Its happening now");
} else {
if (dur.getSeconds() > 0 && dur.getSeconds() < 60) {
Log.d("DINKAR", String.format("Match will start in %d seconds", dur.getSeconds()));
} else if (dur.toMinutes() > 0 && dur.toMinutes() < 60) {
Log.d("DINKAR", String.format("Match will start in %d minutes", dur.toMinutes()));
} else if (dur.toHours() > 0 && dur.toHours() < 24) {
Log.d("DINKAR", String.format("Match will start in %d hours", dur.toHours()));
} else if (dur.toDays() > 0) {
Log.d("DINKAR", String.format("Match will start in %d days", dur.toDays()));
}
}
For API below 26 Please also add the following in your build.gradle
android {
defaultConfig {
// Required when setting minSdkVersion to 20 or lower
multiDexEnabled = true
}
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled = true
// Sets Java compatibility to Java 8
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")
}