Solution using java.time API (Java 8+)
The solution would be clean and clear using the modern date-time API.
Demo:
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
// Tests
System.out.println(isBetween(Instant.parse("2022-11-20T20:30:00Z"), Instant.parse("2022-11-15T23:45:00Z")));
System.out.println(isBetween(Instant.parse("2022-11-20T10:20:30Z"), Instant.parse("2022-11-15T20:10:05Z")));
System.out.println(isBetween(Instant.parse("2022-11-20T10:20:30Z"), Instant.parse("2022-11-15T10:20:30Z")));
}
static boolean isBetween(Instant startInstant, Instant endInstant) {
LocalTime currentTime = getTime(Instant.now());
LocalTime startTime = getTime(startInstant);
LocalTime endTime = getTime(endInstant);
return currentTime.isAfter(startTime) && currentTime.isBefore(endTime);
}
static LocalTime getTime(Instant instant) {
return instant.atZone(ZoneId.systemDefault()).toLocalTime();
}
}
Output when I ran it at ~ 2022-11-30T23:32:00Z:
true
false
false
Learn more about the modern Date-Time API from Trail: Date Time.
Solution using Java version < 8
If your project is open to using 3rd party library for date-time, I recommend you check How to use ThreeTenABP in Android Project. The ThreeTenABP library makes it possible to backport java.time
API to Java 6 and 7.
If you want to stick to the standard library of Java version < 8, here is a solution:
The trick is to set today's year, month and day into the startDate and endDate and then you can compare them as usual. If you have year, month and day the same across all three instances of Date
, basically you comparing just their times.
Demo:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Main {
public static void main(String[] args) throws ParseException {
// Tests
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(isBetween(sdf.parse("2022-11-20T20:30:00Z"), sdf.parse("2022-11-15T23:45:00Z")));
System.out.println(isBetween(sdf.parse("2022-11-20T10:20:30Z"), sdf.parse("2022-11-15T20:10:05Z")));
System.out.println(isBetween(sdf.parse("2022-11-20T10:20:30Z"), sdf.parse("2022-11-15T10:20:30Z")));
}
static boolean isBetween(Date startTime, Date endTime) {
Date currentTime = new Date();
// System.out.println(currentTime);
Calendar currentCal = Calendar.getInstance();
currentCal.setTime(currentTime);
int year = currentCal.get(Calendar.YEAR);
int month = currentCal.get(Calendar.MONTH);
int day = currentCal.get(Calendar.DAY_OF_MONTH);
// Set today's year, month and day into the startDate
Calendar startCal = Calendar.getInstance();
startCal.setTime(startTime);
startCal.set(year, month, day);
startTime = startCal.getTime();
// System.out.println(startTime);
// Set today's year, month and day into the endDate
Calendar endCal = Calendar.getInstance();
endCal.setTime(endTime);
endCal.set(year, month, day);
endTime = endCal.getTime();
// System.out.println(endTime);
return currentTime.after(startTime) && currentTime.before(endTime);
}
}
Output when I ran it at ~ 2022-11-30T23:33:00Z:
true
false
false