I have timespans:
String time1 = 01:00:00
String time2 = 05:00:00
I want to check if time1 and time2 both lies between 20:11:13 and 14:49:00.
Actually, 01:00:00 is greater than 20:11:13 and less than 14:49:00 considering 20:11:13 is always less than 14:49:00. This is given prerequisite.
So what I want is, 20:11:13 < 01:00:00 < 14:49:00.
So I need something like that:
public void getTimeSpans()
{
boolean firstTime = false, secondTime = false;
if(time1 > "20:11:13" && time1 < "14:49:00")
{
firstTime = true;
}
if(time2 > "20:11:13" && time2 < "14:49:00")
{
secondTime = true;
}
}
I know that this code does not give correct result as I am comparing the string objects.
How to do that as they are the timespans but not the strings to compare?
You can use the Calendar class in order to check.
For example:
try {
String string1 = "20:11:13";
Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
calendar1.add(Calendar.DATE, 1);
String string2 = "14:49:00";
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
String someRandomTime = "01:00:00";
Date d = new SimpleDateFormat("HH:mm:ss").parse(someRandomTime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
//checkes whether the current time is between 14:49:00 and 20:11:13.
System.out.println(true);
}
} catch (ParseException e) {
e.printStackTrace();
}
tl;dr
20:11:13 < 01:00:00 < 14:49:00
LocalTime target = LocalTime.parse( "01:00:00" ) ;
Boolean targetInZone = (
target.isAfter( LocalTime.parse( "20:11:13" ) )
&&
target.isBefore( LocalTime.parse( "14:49:00" ) )
) ;
java.time.LocalTime
The java.time classes include LocalTime to represent a time-of-day only without a date and without a time zone.
So what I want is, 20:11:13 < 01:00:00 < 14:49:00.
First we define the boundaries. Your input strings happen to comply with standard ISO 8601 formats. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.
LocalTime start = LocalTime.parse( "20:11:13" );
LocalTime stop = LocalTime.parse( "14:49:00" );
And define our test case, the target 01:00:00.
LocalTime target = LocalTime.parse( "01:00:00" );
Now we are set up to compare these LocalTime objects. We want to see if the target is after the later time but before the earlier time. That means middle of the night in this case, between approximately 8 PM and 3 AM the next morning.
Boolean isTargetAfterStartAndBeforeStop = ( target.isAfter( start ) && target.isBefore( stop ) ) ;
That test can be more simply stated as “not between 3 AM and 8 PM”. We could then generalize to any pair of LocalTime objects where we test for between if the start comes before the stop with a 24-hour clock, and not between if start comes after the stop (as in the case of this Question).
Further more, spans of time are usually handled with the Half-Open approach where the beginning is inclusive while the ending is exclusive. So a "between" comparison, strictly speaking, would be “is the target equal to or later than start AND the target is before stop”, or more simply, “is target not before start AND before stop”.
Boolean isBetweenStartAndStopStrictlySpeaking =
( ( ! target.isBefore( start ) && target.isBefore( stop ) ) ;
If the start is after the stop, within a 24-hour clock, then assume we want the logic suggested in the Question (is after 8 PM but before 3 AM).
if( start.isAfter( stop ) ) {
return ! isBetweenStartAndStopStrictlySpeaking ;
} else {
return isBetweenStartAndStopStrictlySpeaking ;
}
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.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
Java 9 brought some minor features and fixes.
Java SE 6 and Java SE 7
Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android (26+) bundle implementations of the java.time classes.
For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….
The answer given by #kocko works in only same day.
If start time "23:00:00" and end "02:00:00"[next day] and current time is "01:30:00" then result will false...
I modified the #kocko's answer to work perfectly
public static boolean isTimeBetweenTwoTime(String initialTime, String finalTime,
String currentTime) throws ParseException {
String reg = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if (initialTime.matches(reg) && finalTime.matches(reg) &&
currentTime.matches(reg))
{
boolean valid = false;
//Start Time
//all times are from java.util.Date
Date inTime = new SimpleDateFormat("HH:mm:ss").parse(initialTime);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(inTime);
//Current Time
Date checkTime = new SimpleDateFormat("HH:mm:ss").parse(currentTime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(checkTime);
//End Time
Date finTime = new SimpleDateFormat("HH:mm:ss").parse(finalTime);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(finTime);
if (finalTime.compareTo(initialTime) < 0)
{
calendar2.add(Calendar.DATE, 1);
calendar3.add(Calendar.DATE, 1);
}
java.util.Date actualTime = calendar3.getTime();
if ((actualTime.after(calendar1.getTime()) ||
actualTime.compareTo(calendar1.getTime()) == 0) &&
actualTime.before(calendar2.getTime()))
{
valid = true;
return valid;
} else {
throw new IllegalArgumentException("Not a valid time, expecting
HH:MM:SS format");
}
}
}
Output
"07:00:00" - "17:30:00" - "15:30:00" [current] - true
"17:00:00" - "21:30:00" - "16:30:00" [current] - false
"23:00:00" - "04:00:00" - "02:00:00" [current] - true
"00:30:00" - "06:00:00" - "06:00:00" [current] - false
(I have included lower limit value to [upper limit value-1])
Modified #Surendra Jnawali' code. It fails
if current time is 23:40:00 i.e greater than start time and less than equals to 23:59:59.
All credit goes to the real owner
This is how it should be :This works perfect
public static boolean isTimeBetweenTwoTime(String argStartTime,
String argEndTime, String argCurrentTime) throws ParseException {
String reg = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
//
if (argStartTime.matches(reg) && argEndTime.matches(reg)
&& argCurrentTime.matches(reg)) {
boolean valid = false;
// Start Time
java.util.Date startTime = new SimpleDateFormat("HH:mm:ss")
.parse(argStartTime);
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startTime);
// Current Time
java.util.Date currentTime = new SimpleDateFormat("HH:mm:ss")
.parse(argCurrentTime);
Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(currentTime);
// End Time
java.util.Date endTime = new SimpleDateFormat("HH:mm:ss")
.parse(argEndTime);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endTime);
//
if (currentTime.compareTo(endTime) < 0) {
currentCalendar.add(Calendar.DATE, 1);
currentTime = currentCalendar.getTime();
}
if (startTime.compareTo(endTime) < 0) {
startCalendar.add(Calendar.DATE, 1);
startTime = startCalendar.getTime();
}
//
if (currentTime.before(startTime)) {
System.out.println(" Time is Lesser ");
valid = false;
} else {
if (currentTime.after(endTime)) {
endCalendar.add(Calendar.DATE, 1);
endTime = endCalendar.getTime();
}
System.out.println("Comparing , Start Time /n " + startTime);
System.out.println("Comparing , End Time /n " + endTime);
System.out
.println("Comparing , Current Time /n " + currentTime);
if (currentTime.before(endTime)) {
System.out.println("RESULT, Time lies b/w");
valid = true;
} else {
valid = false;
System.out.println("RESULT, Time does not lies b/w");
}
}
return valid;
} else {
throw new IllegalArgumentException(
"Not a valid time, expecting HH:MM:SS format");
}
}
RESULT
Comparing , Start Time /n Thu Jan 01 23:00:00 IST 1970
Comparing , End Time /n Fri Jan 02 02:00:00 IST 1970
Comparing , Current Time /n Fri Jan 02 01:50:00 IST 1970
RESULT, Time lies b/w
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY); // Get hour in 24 hour format
int minute = now.get(Calendar.MINUTE);
Date date = parseDate(hour + ":" + minute);
Date dateCompareOne = parseDate("08:00");
Date dateCompareTwo = parseDate("20:00");
if (dateCompareOne.before( date ) && dateCompareTwo.after(date)) {
//your logic
}
private Date parseDate(String date) {
final String inputFormat = "HH:mm";
SimpleDateFormat inputParser = new SimpleDateFormat(inputFormat, Locale.US);
try {
return inputParser.parse(date);
} catch (java.text.ParseException e) {
return new Date(0);
}
}
Further more, to be more precise,
If you compare a time between an interval more than 00:00 to 24:00 of that day,
you need to parse the day too.
There are lots of answers here but I want to provide a new one which is similar with Basil Bourque's answer but with a full code example. So please see the method below:
private static void checkTime(String startTime, String endTime, String checkTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.US);
LocalTime startLocalTime = LocalTime.parse(startTime, formatter);
LocalTime endLocalTime = LocalTime.parse(endTime, formatter);
LocalTime checkLocalTime = LocalTime.parse(checkTime, formatter);
boolean isInBetween = false;
if (endLocalTime.isAfter(startLocalTime)) {
if (startLocalTime.isBefore(checkLocalTime) && endLocalTime.isAfter(checkLocalTime)) {
isInBetween = true;
}
} else if (checkLocalTime.isAfter(startLocalTime) || checkLocalTime.isBefore(endLocalTime)) {
isInBetween = true;
}
if (isInBetween) {
System.out.println("Is in between!");
} else {
System.out.println("Is not in between!");
}
}
Either if you are calling this method using:
checkTime("20:11:13", "14:49:00", "01:00:00");
Or using:
checkTime("20:11:13", "14:49:00", "05:00:00");
The result will be:
Is in between!
Following method checks whether 'validateTime' is between 'startTime' & 'endTime' or not while considering possibility that 'endTime' can be a next day. To use it properly parse your dates in "HH:mm" formant.
public static final boolean isBetweenValidTime(Date startTime, Date endTime, Date validateTime)
{
boolean validTimeFlag = false;
if(endTime.compareTo(startTime) <= 0)
{
if(validateTime.compareTo(endTime) < 0 || validateTime.compareTo(startTime) >= 0)
{
validTimeFlag = true;
}
}
else if(validateTime.compareTo(endTime) < 0 && validateTime.compareTo(startTime) >= 0)
{
validTimeFlag = true;
}
return validTimeFlag;
}
Java 8 - LocalDateTime
What about this?
final LocalDateTime now = LocalDateTime.now();
final LocalDateTime minRange = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 22, 30); //Today, 10:30pm
LocalDateTime maxRange = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 6, 30); //Tomorrow, 6:30am
maxRange = maxRange.plusDays(1); //Ensures that you don't run into an exception if minRange is the last day in the month.
if (now.isAfter(minRange) && now.isBefore(maxRange)) {
//Action
}
Using LocalTime would simply ignore the Date value:
public class TimeIntervalChecker {
static final LocalTime time1 = LocalTime.parse( "20:11:13" ) ;
static final LocalTime time2 = LocalTime.parse( "14:49:00" ) ;
public static void main(String[] args) throws java.lang.Exception {
LocalTime nowUtcTime = LocalTime.now(Clock.systemUTC());
if (nowUtcTime.isAfter(time1) && nowUtcTime.isBefore(time2)){
System.out.println(nowUtcTime+" is after: "+ time1+" and before: "+ time2);
}
}
After reading a few replies, I feel the writing is too complicated. Try my code
public static boolean compare(String system_time, String currentTime, String endtimes) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
Date startime = simpleDateFormat.parse("19:25:00");
Date endtime = simpleDateFormat.parse("20:30:00");
//current time
Date current_time = simpleDateFormat.parse("20:00:00");
if (current_time.after(startime) && current_time.before(endtime)) {
System.out.println("Yes");
return true;
}
else if (current_time.after(startime) && current_time.after(endtime)) {
return true; //overlap condition check
}
else {
System.out.println("No");
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
As with the help of #kocko, the complete working code is as below:
try{
Date time11 = new SimpleDateFormat("HH:mm:ss").parse("20:11:13");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time11);
Date time22 = new SimpleDateFormat("HH:mm:ss").parse("14:49:00");
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time22);
Date currentTime = new SimpleDateFormat("HH:mm:ss").parse("00:00:00");
Calendar startingCalendar = Calendar.getInstance();
startingCalendar.setTime(currentTime);
startingCalendar.add(Calendar.DATE, 1);
//let's say we have to check about 01:00:00
String someRandomTime = time1;
Date d = new SimpleDateFormat("HH:mm:ss").parse(someRandomTime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
if(startingCalendar.getTime().after(calendar1.getTime()))
{
calendar2.add(Calendar.DATE, 1);
calendar3.add(Calendar.DATE, 1);
}
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime()))
{
System.out.println("Time is in between..");
}
else
{
System.out.println("Time is not in between..");
}
} catch (ParseException e)
{
e.printStackTrace();
}
Sounds to me that your problem is an OR situation... You want to check if time1 > 20:11:13 OR time1 < 14:49:00.
There will never be a time greater to 20:11:13 that exceeds your range through the other end (14:49:00) and viceversa. Think of it as if you are checking that a time is NOT between a properly ordered couple of timestamps.
The Actual working function will be as follows
public static boolean isTimeBetweenTwoTime(Date startTime, Date stopTime, Date currentTime) {
//Start Time
Calendar StartTime = Calendar.getInstance();
StartTime.setTime(startTime);
//Current Time
Calendar CurrentTime = Calendar.getInstance();
CurrentTime.setTime(currentTime);
//Stop Time
Calendar StopTime = Calendar.getInstance();
StopTime.setTime(stopTime);
if (stopTime.compareTo(startTime) < 0) {
if (CurrentTime.compareTo(StopTime) < 0) {
CurrentTime.add(Calendar.DATE, 1);
}
StopTime.add(Calendar.DATE, 1);
}
return CurrentTime.compareTo(StartTime) >= 0 && CurrentTime.compareTo(StopTime) < 0;
}
In your case the starting time (20:11:13) is larger than the ending time (14:49:00). It is a reasonable assumption that you could solve the problem by adding a day on the ending time or subtracting a day from the starting time. if you do so, you will be trapped because you do not know on which day the testing time is.
You can avoid this trap by checking whether your testing time is between the ending time and starting time. If true, then result is "not in between"; else result is "well in between".
Here is the function in JAVA I have been using. It works so far for me. Good luck.
boolean IsTimeInBetween(Calendar startC, Calendar endC, Calendar testC){
// assume year, month and day of month are all equal.
startC.set(1,1,1);
endC.set(1,1,1);
testC.set(1,1,1);
if (endC.compareTo(startC) > 0) {
if ((testC.compareTo(startC)>=0) && (testC.compareTo(endC)<=0)) {
return true;
}else {
return false;
}
}else if (endC.compareTo(startC) < 0) {
if ((testC.compareTo(endC) >= 0) && (testC.compareTo(startC) <= 0)) {
return false;
} else {
return true;
}
} else{ // when endC.compareTo(startC)==0, I return a ture value. Change it if you have different application.
return true;
}
}
To create a Calender instance you can use:
Calendar startC = Calendar.getInstance();
startC.set(Calendar.HOUR_OF_DAY, 20);
startC.set(Calendar.MINUTE,11);
startC.set(Calendar.SECOND,13);
In the code snipet below, it is being verified that if the current time (can be any) exists between start and end time or not:
Calendar startTimeCal = Calendar.getInstance();
startTimeCal.setTime(startTime);
int startTimeHour = startTimeCal.get(Calendar.HOUR_OF_DAY);
if (startTimeHour == 0){
startTimeHour = 24;
}
int startTimeMinutes = startTimeCal.get(Calendar.MINUTE);
Calendar curTimeCal = Calendar.getInstance();
curTimeCal.setTime(currentTime);
int curTimeHour = curTimeCal.get(Calendar.HOUR_OF_DAY);
int curTimeMinutes = curTimeCal.get(Calendar.MINUTE);
Calendar endTimeCal = Calendar.getInstance();
endTimeCal.setTime(endTime);
int endTimeHour = endTimeCal.get(Calendar.HOUR_OF_DAY);
if (endTimeHour == 0) {
endTimeHour = 24;
}
int endTimeMinutes = endTimeCal.get(Calendar.MINUTE);
if (((curTimeHour > startTimeHour) || (curTimeHour == startTimeHour && curTimeMinutes >= startTimeMinutes)) &&
((curTimeHour < endTimeHour) || (curTimeHour == endTimeHour && curTimeMinutes <= endTimeHour))) {
//time exists between start and end time
} else {
//time doesn't exist between start and end time
}
As many people noticed, it's not a date problem, it's a logic problem.
Let's assume a day is splitted in two intervals: one lies between 20:11:13 and 14:49:00, while the other lies between 14:49:00 and 20:11:13 (which interval the extremes belong is up to you).
If you want to check if a certain time is included in the 20:11:13/14:49:00 one, the one you're interested of, just check if it's included in the other one, 14:49:00/20:11:13, which is much easier because the natural order of the numbers, and then negate the result.
I did it this way:
LocalTime time = LocalTime.now();
if (time.isAfter(LocalTime.of(02, 00)) && (time.isBefore(LocalTime.of(04, 00))))
{
log.info("Checking after 2AM, before 4AM!");
}
Edit:
String time1 = "01:00:00";
String time2 = "15:00:00";
LocalTime time = LocalTime.parse(time2);
if ((time.isAfter(LocalTime.of(20,11,13))) || (time.isBefore(LocalTime.of(14,49,0))))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
Here is a solution that uses the new Java 8 classes, is compact, requires no regular expressions or manual arithmetic operations. My solution is coded for inclusive startTime and exclusive endTime, but can easily be modified for your needs.
private boolean isTimeBetween(String timeToTest, String startTime, String endTime) {
LocalTime timeToTestDt = LocalTime.parse(timeToTest, DateTimeFormatter.ISO_LOCAL_TIME);
LocalTime startTimeDt = LocalTime.parse(startTime, DateTimeFormatter.ISO_LOCAL_TIME);
LocalTime endTimeDt = LocalTime.parse(endTime, DateTimeFormatter.ISO_LOCAL_TIME);
if(startTime.equals(endTime)) {
return false;
}
else if(startTimeDt.isBefore(endTimeDt)) { // Period does not cross the day boundary
return (timeToTest.equals(startTime) || timeToTestDt.isAfter(startTimeDt))
&& timeToTestDt.isBefore(endTimeDt);
} else { // Time period spans two days, e.g. 23:00 to 2:00
return (!((timeToTestDt.isAfter(endTimeDt) || timeToTest.equals(endTime))
&& timeToTestDt.isBefore(startTimeDt)));
}
}
// getTimeSpans() from the original question would then look like this
public void getTimeSpans()
{
boolean firstTime = isTimeBetween("01:00:00", "20:11:13", "14:49:00");
boolean secondTime = isTimeBetween("05:00:00", "20:11:13", "14:49:00");
}
Simple solution for all gaps:
public boolean isNowTimeBetween(String startTime, String endTime) {
LocalTime start = LocalTime.parse(startTime);//"22:00"
LocalTime end = LocalTime.parse(endTime);//"10:00"
LocalTime now = LocalTime.now();
if (start.isBefore(end))
return now.isAfter(start) && now.isBefore(end);
return now.isBefore(start)
? now.isBefore(start) && now.isBefore(end)
: now.isAfter(start) && now.isAfter(end);
}
I've implemented it in kotlin, and it's works as expected:
fun isInBetween(startTime: String, endTime: String, checkTime: String, timeFormat: String = "HH:mm:ss"): Boolean {
val calendar1 = Calendar.getInstance().apply {
time = SimpleDateFormat(timeFormat, Locale.ENGLISH).parse(startTime)!!
add(Calendar.DATE, 1)
}
val calendar2 = Calendar.getInstance().apply {
time = SimpleDateFormat(timeFormat, Locale.ENGLISH).parse(endTime)!!
add(Calendar.DATE, 1)
}
val calendar3 = Calendar.getInstance().apply {
time = SimpleDateFormat(timeFormat, Locale.ENGLISH).parse(checkTime)!!
add(Calendar.DATE, 1)
}
if(calendar1.time > calendar2.time) {
calendar2.add(Calendar.DATE, 2)
calendar3.add(Calendar.DATE, 2)
}
val x = calendar3.time
return (x.after(calendar1.time) && x.before(calendar2.time))
}
And it's result as followings:
Log.d("TAG", "08:00, 22:00, 13:40: ${isInBetween("08:00", "22:00", "13:40")}") // true
Log.d("TAG", "22:00, 08:00, 13:40: ${isInBetween("22:00", "08:00", "13:40")}") // false
Log.d("TAG", "22:00, 08:00, 05:40: ${isInBetween("22:00", "08:00", "05:40")}") // true
Log.d("TAG", "22:00, 08:00, 10:40: ${isInBetween("22:00", "08:00", "10:40")}") // false
Log.d("TAG", "22:00, 22:00, 22:10: ${isInBetween("22:00", "22:00", "22:10")}") // false
In my situation, I'm not interested at all in date times.
So this is my solution which works solely on hours as integers:
boolean isInTimeRange(int startingHour, int endingHour, int hourOfDayToCheck) {
if (endingHour > startingHour) {
return hourOfDayToCheck >= startingHour && hourOfDayToCheck < endingHour;
} else {
return hourOfDayToCheck >= startingHour || hourOfDayToCheck < endingHour;
}
}
LocalTime now = LocalTime.now();
LocalTime startTime = LocalTime.parse("23:00:00");
LocalTime endTime = LocalTime.parse("05:00:00");
if (startTime.isAfter(endTime)) {
return !(now.isAfter(endTime) && now.isBefore(startTime));
} else {
return (now.isAfter(startTime) && now.isBefore(endTime));
}
The below solution should help in cases where StartTime is from the Previous Day, and EndTime is from the next day, and you want to check something in between.
boolean isInBetween = false;
if (endLocalTime.isAfter(startLocalTime)) {
if (!startLocalTime.isAfter(checkLocalTime) && endLocalTime.isAfter(checkLocalTime)) {
isInBetween = true;
}
} else if (checkLocalTime.isAfter(startLocalTime) || !checkLocalTime.isAfter(endLocalTime)) {
isInBetween = true;
}
return isInBetween;
}
strip colons from the $time, $to and $from strings, convert to int and then use the following condition to check if the time is between from and to. Example is in php, but shouldn't matter.
if(($to < $from && ($time >= $from || $time <= $to)) ||
($time >= $from && $time <= $to)) {
return true;
}
Logically if you do the following you should always be ok granted we use military time...
if start time is greater than end time add 24 to end time
else use times as is
compare current time to be inbetween start and end time.
Based on the ideas and solutions of most authors here, I'd like to share my refined solution with a presumably cleaner code:
/**
* Checks if some date is within a time window given by start and end dates
*
* #param checkDate - date to check if its hours and minutes is between the startDate and endDate
* #param startDate - startDate of the time window
* #param endDate - endDate of the time window
* #return - returns true if hours and minutes of checkDate is between startDate and endDate
*/
public static boolean isDateBetweenStartAndEndHoursAndMinutes(Date checkDate, Date startDate, Date endDate) {
if (startDate == null || endDate == null)
return false;
LocalDateTime checkLdt = LocalDateTime.ofInstant(Instant.ofEpochMilli(checkDate.getTime()), ZoneId.systemDefault());
LocalDateTime startLdt = LocalDateTime.ofInstant(Instant.ofEpochMilli(startDate.getTime()), ZoneId.systemDefault());
LocalDateTime endLdt = LocalDateTime.ofInstant(Instant.ofEpochMilli(endDate.getTime()), ZoneId.systemDefault());
// Table of situations:
// Input dates: start (a), end (b), check (c)
// Interpretations:
// t(x) = time of point x on timeline; v(x) = nominal value of x
// Situation A - crossing midnight:
// c INSIDE
// 1) t(a) < t(c) < t(b) | v(b) < v(a) < v(c) // e.g. a=22:00, b=03:00, c=23:00 (before midnight)
// 2) t(a) < t(c) < t(b) | v(c) < v(b) < v(a) // e.g. a=22:00, b=03:00, c=01:00 (after midnight)
// c OUTSIDE
// 3) t(c) < t(a) < t(b) | v(b) < v(c) < v(a) // e.g. a=22:00, b=03:00, c=21:00
// 4) t(a) < t(b) < t(c) | v(b) < v(c) < v(a) // e.g. a=22:00, b=03:00, c=04:00
// ^--- v(b) < v(a) always when shift spans around midnight!
// Situation B - after/before midnight:
// c INSIDE
// 1) t(a) = t(c) < t(b) | v(a) = v(c) < v(b) // e.g. a=06:00, b=14:00, c=06:00
// 2) t(a) < t(c) < t(b) | v(a) < v(c) < v(b) // e.g. a=06:00, b=14:00, c=08:00
// c OUTSIDE
// 3) t(c) < t(a) < t(b) | v(c) < v(a) < v(b) // e.g. a=06:00, b=14:00, c=05:00
// 4) t(a) < t(b) = t(c) | v(a) < v(b) = v(c) // e.g. a=06:00, b=14:00, c=14:00
// 5) t(a) < t(b) < t(c) | v(a) < v(b) < v(c) // e.g. a=06:00, b=14:00, c=15:00
// ^--- v(a) < v(b) if shift starts after midnight and ends before midnight!
// Check for situation A - crossing midnight?
boolean crossingMidnight = endLdt.isBefore(startLdt);
if (crossingMidnight) {
// A.1
if ((startLdt.isBefore(checkLdt) || startLdt.isEqual(checkLdt)) // t(a) < t(c)
&& checkLdt.isBefore(endLdt.plusDays(1))) // t(c) < t(b+1D)
return true;
// A.2
if (startLdt.isBefore(checkLdt.plusDays(1)) // t(a) < t(c+1D)
&& checkLdt.isBefore(endLdt)) // t(c) < t(b)
return true;
// A.3
if (startLdt.isBefore(endLdt.plusDays(1)) // t(a) < t(b+1D)
&& checkLdt.isBefore(startLdt)) // t(c) < t(a)
return false;
// A.4
if (startLdt.isBefore(endLdt.plusDays(1)) // t(a) < t(b+1D)
&& checkLdt.isAfter(endLdt)) // t(b) < t(c)
return false;
} else {
// B.1 + B.2
if ((startLdt.isEqual(checkLdt) || startLdt.isBefore(checkLdt)) // t(a) = t(c) || t(a) < t(c)
&& checkLdt.isBefore(endLdt)) // t(c) < t(b)
return true;
}
return false;
}
For the sake of completeness I've added the conditions of A.3 and A.4, but in productive code you can leave it out.
Now you can simply create your start and end dates, as well as your time you want to check and call this static method. The code would go then as follows:
Date check = new SimpleDateFormat("HH:mm:ss").parse("01:00:00");
Date start = new SimpleDateFormat("HH:mm:ss").parse("20:11:13");
Date end = new SimpleDateFormat("HH:mm:ss").parse("14:49:00");
if (isDateBetweenStartAndEndHoursAndMinutes(check, start, end)) {
Print("checkDate is within start and End date!"); // adjust this true condition to your needs
}
For the TDD aspect I've added unit tests for the scenarios A and B as given above. Please feel free to check it out and report back if you find any errors or spots for optimization.
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
class LogiqDateUtilsTest {
private LocalDateTime startShiftSituationALdt = LocalDateTime.of(0, 1, 1, 22, 0);
private Date startOfShiftSituationA = Date.from(startShiftSituationALdt.atZone(ZoneId.systemDefault()).toInstant());
private LocalDateTime endShiftSituationALdt = LocalDateTime.of(0, 1, 1, 3, 0);
private Date endOfShiftSituationA = Date.from(endShiftSituationALdt.atZone(ZoneId.systemDefault()).toInstant());
private LocalDateTime startShiftSituationBLdt = LocalDateTime.of(0, 1, 1, 6, 0);
private Date startOfShiftSituationB = Date.from(startShiftSituationBLdt.atZone(ZoneId.systemDefault()).toInstant());
private LocalDateTime endShiftSituationBLdt = LocalDateTime.of(0, 1, 1, 14, 0);
private Date endOfShiftSituationB = Date.from(endShiftSituationBLdt.atZone(ZoneId.systemDefault()).toInstant());
#Test
void testSituationA1() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 23, 0);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertTrue(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationA, endOfShiftSituationA));
}
#Test
void testSituationA2() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 1, 0);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertTrue(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationA, endOfShiftSituationA));
}
#Test
void testSituationA3() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 21, 1);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertFalse(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationA, endOfShiftSituationA));
}
#Test
void testSituationA4() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 4, 1);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertFalse(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationA, endOfShiftSituationA));
}
#Test
void testSituationB1() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 6, 0);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertTrue(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationB, endOfShiftSituationB));
}
#Test
void testSituationB2() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 8, 0);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertTrue(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationB, endOfShiftSituationB));
}
#Test
void testSituationB3() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 5, 0);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertFalse(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationB, endOfShiftSituationB));
}
#Test
void testSituationB4() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 14, 0);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertFalse(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationB, endOfShiftSituationB));
}
#Test
void testSituationB5() {
LocalDateTime checkLdt = LocalDateTime.of(0, 1, 1, 15, 0);
Date checkBetween = Date.from(checkLdt.atZone(ZoneId.systemDefault()).toInstant());
assertFalse(isDateBetweenStartAndEndHoursAndMinutes(checkBetween, startOfShiftSituationB, endOfShiftSituationB));
}
}
Cheers!
/**
* #param initialTime - in format HH:mm:ss
* #param finalTime - in format HH:mm:ss
* #param timeToCheck - in format HH:mm:ss
* #return initialTime <= timeToCheck < finalTime
* #throws IllegalArgumentException if passed date with wrong format
*/
public static boolean isTimeBetweenTwoTime(String initialTime, String finalTime, String timeToCheck) throws IllegalArgumentException {
String reg = "^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";
if (initialTime.matches(reg) && finalTime.matches(reg) && timeToCheck.matches(reg)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
Date inTime = parseDate(dateFormat, initialTime);
Date finTime = parseDate(dateFormat, finalTime);
Date checkedTime = parseDate(dateFormat, timeToCheck);
if (finalTime.compareTo(initialTime) < 0) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(finTime);
calendar.add(Calendar.DAY_OF_YEAR, 1);
finTime = calendar.getTime();
if (timeToCheck.compareTo(initialTime) < 0) {
calendar.setTime(checkedTime);
calendar.add(Calendar.DAY_OF_YEAR, 1);
checkedTime = calendar.getTime();
}
}
return (checkedTime.after(inTime) || checkedTime.compareTo(inTime) == 0) && checkedTime.before(finTime);
} else {
throw new IllegalArgumentException("Not a valid time, expecting HH:MM:SS format");
}
}
/**
* #param initialTime - in format HH:mm:ss
* #param finalTime - in format HH:mm:ss
* #return initialTime <= now < finalTime
* #throws IllegalArgumentException if passed date with wrong format
*/
public static boolean isNowBetweenTwoTime(String initialTime, String finalTime) throws IllegalArgumentException {
return isTimeBetweenTwoTime(initialTime, finalTime,
String.valueOf(DateFormat.format("HH:mm:ss", new Date()))
);
}
private static Date parseDate(SimpleDateFormat dateFormat, String data) {
try {
return dateFormat.parse(data);
} catch (ParseException e) {
throw new IllegalArgumentException("Not a valid time");
}
}
This worked for me:
fun timeBetweenInterval(
openTime: String,
closeTime: String
): Boolean {
try {
val dateFormat = SimpleDateFormat(TIME_FORMAT)
val afterCalendar = Calendar.getInstance().apply {
time = dateFormat.parse(openTime)
add(Calendar.DATE, 1)
}
val beforeCalendar = Calendar.getInstance().apply {
time = dateFormat.parse(closeTime)
add(Calendar.DATE, 1)
}
val current = Calendar.getInstance().apply {
val localTime = dateFormat.format(timeInMillis)
time = dateFormat.parse(localTime)
add(Calendar.DATE, 1)
}
return current.time.after(afterCalendar.time) && current.time.before(beforeCalendar.time)
} catch (e: ParseException) {
e.printStackTrace()
return false
}
}
Based on Konstantin_Yovkov answer I would like to share my implementation which checks if current time will be in between given START and END time.
This implementation assumes that if given END time is 'before' the START time, then END must be meant to be tomorrow:
public static boolean currentTimeInBetween(String start, String end)
throws ParseException {
// start = "15:25";
java.util.Date starttime = new SimpleDateFormat("HH:mm").parse(start);
Calendar startcal = Calendar.getInstance();
startcal.setTime(starttime);
// end = "14:00";
java.util.Date endtime = new SimpleDateFormat("HH:mm").parse(end);
Calendar endcal = Calendar.getInstance();
endcal.setTime(endtime);
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
java.util.Date currenttime = dateFormat
.parse(dateFormat.format(new java.util.Date()));
Calendar currentcal = Calendar.getInstance();
currentcal.setTime(currenttime);
// If endTime < startTime, assume that endTime is 'tomorrow'
if (startcal.after(endcal)) {
endcal.add(Calendar.DATE, 1);
}
// System.out.println("START" + " System Date: " + startcal.getTime());
// System.out.println("END" + " System Date: " + endcal.getTime());
// System.out.println("Current" + " System Date: " + currentcal.getTime());
java.util.Date current = currentcal.getTime();
if (current.after(startcal.getTime())
&& current.before(endcal.getTime())) {
return true;
} else {
return false;
}
}
Solution function written in Kotlin
/**
* #param currentTime : Time to compare
* #param startTime: Start Hour in format like 10:00:00
* #param endTime: End Hour in format like 15:45:00
*/
fun isTimeInBetweenHours(currentDate: Date, startTime: String, endTime: String): Boolean {
val simpleDateFormat = SimpleDateFormat("HH:mm:ss", Locale.US)
try {
val startTimeCalendar = Calendar.getInstance()
startTimeCalendar.time = simpleDateFormat.parse(startTime)
startTimeCalendar.add(Calendar.DATE, 1)
val endTimeCalendar = Calendar.getInstance()
endTimeCalendar.time = simpleDateFormat.parse(endTime)
endTimeCalendar.add(Calendar.DATE, 1)
val currentTime = simpleDateFormat.format(currentDate) //"01:00:00"
val currentTimeCalendar = Calendar.getInstance()
currentTimeCalendar.time = simpleDateFormat.parse(currentTime)
currentTimeCalendar.add(Calendar.DATE, 1)
val x = currentTimeCalendar.time
return x.after(startTimeCalendar.time) && x.before(endTimeCalendar.time)
} catch (e: ParseException) {
return false
}
}
The formatter only takes HH:mm:ss, so it's agnostic of date. All the dates are computed as 1st Jan 1970 being the epoch start date. Hence the comparison of time happens with only the time as the date for all the cases here is 1st Jan 1970.
Note: Used legacy Java API's instead of the newer ones (LocalTime and DateTimeFormatter) since these newer API's are not supported on older devices like Android below version Oreo. If you are using some other platform where you can get the updated API's, please use them as they are more optimised and less buggy.
Related
I'm trying to check a current date and time is in between Friday 17:42 and Sunday 17:42 of the week with Java.
At the moment I'm doing this with really really bad code block. It was a hurry solution. Now I'm refactoring but I couldn't find any method in joda or etc.
Any ideas?
Thanks
private final Calendar currentDate = Calendar.getInstance();
private final int day = currentDate.get(Calendar.DAY_OF_WEEK);
private final int hour = currentDate.get(Calendar.HOUR_OF_DAY);
private final int minute = currentDate.get(Calendar.MINUTE);
if (day != 1 && day != 6 && day != 7) {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
} else {
if (day == 6 && hour > 16) {
if (hour == 17 && minute < 43) {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
} else {
return badge == 0;
}
} else if (day == 6 && hour < 17) {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
} else if (day == 1 && hour > 16) {
if (hour == 17 && minute < 43) {
return badge == 0;
} else {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
}
} else {
return badge == 0;
}
}
I've used the solution like thiswith the help of #MadProgrammer and #Meno Hochschild
Method:
public static boolean isBetween(LocalDateTime check, LocalDateTime startTime, LocalDateTime endTime) {
return ((check.equals(startTime) || check.isAfter(startTime)) && (check.equals(endTime) || check.isBefore(endTime))); }
Usage:
static LocalDateTime now = LocalDateTime.now();
static LocalDateTime friday = now.with(DayOfWeek.FRIDAY).toLocalDate().atTime(17, 41);
static LocalDateTime sunday = friday.plusDays(2).plusMinutes(1);
if (!isBetween(now, friday, sunday)) { ... }
Thanks again for your efforts.
Date and Calendar have methods that can perform comparisons on other instances of Date/Calendar, equals, before and after
However, I'd encourage the use of Java 8's new Time API
public static boolean isBetween(LocalDateTime check, LocalDateTime startTime, LocalDateTime endTime) {
return ((check.equals(startTime) || check.isAfter(startTime)) &&
(check.equals(endTime) || check.isBefore(endTime)));
}
Which will return true if the supplied LocalDateTime is within the specified range inclusively.
Something like...
LocalDateTime start = LocalDateTime.now();
start = start.withDayOfMonth(26).withHour(17).withMinute(42).withSecond(0).withNano(0);
LocalDateTime end = start.plusDays(2);
LocalDateTime check = LocalDateTime.now();
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = start;
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = end;
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = start.plusDays(1);
System.out.println(check + " is within range = " + isBetween(check, start, end));
check = end.plusMinutes(1);
System.out.println(check + " is within range = " + isBetween(check, start, end));
Which outputs
2015-06-25T18:31:32.969 is within range = false
2015-06-26T17:42 is within range = true
2015-06-28T17:42 is within range = true
2015-06-27T17:42 is within range = true
2015-06-28T17:43 is within range = false
Joda-Time has an Interval class which makes it even eaiser
Interval targetInterval = new Interval(targetStart, targetEnd);
System.out.println("Contains interval = " + interval.contains(targetInterval)
which is demonstrated here
A different approach...
So I was thinking on way home, assuming all you have is the date/time you want to check, how you might determine if the day falls within your range
LocalDateTime now = LocalDateTime.now();
boolean isBetween = false;
switch (now.getDayOfWeek()) {
case FRIDAY:
case SATURDAY:
case SUNDAY:
LocalDateTime lastFriday = getLastFriday(now);
LocalDateTime nextSunday = getNextSunday(now);
isBetween = isBetween(now, lastFriday, nextSunday);
System.out.println(lastFriday + " - " + nextSunday + ": " + end);
break;
}
What this does is checks the dayOfWeek to see if it's within the desired range, if it is, it finds the previous Friday and next Sunday from the specified date and checks to see if it falls between them (see the previous example)
lastFriday and nextSunday simply adds/subtracts a day from the specified date/time until to reaches the desired dayOfWeek, it then seeds the required time constraints
public static LocalDateTime getLastFriday(LocalDateTime anchor) {
LocalDateTime ldt = LocalDateTime.from(anchor);
return ldt.with(DayOfWeek.FRIDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}
public static LocalDateTime getNextSunday(LocalDateTime anchor) {
LocalDateTime ldt = LocalDateTime.from(anchor);
return ldt.with(DayOfWeek.SUNDAY).withHour(17).withMinute(42).withSecond(0).withNano(0);
}
With Calendar you can know what DAY_OF_WEEK is the given date, then simply check the hours:
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// in friday the hour must be greater than 17:42
if (dayOfWeek == 5 && ((hour > 17) || (hour == 17 && minute >= 42)) {
// successss!!
}
// days from 1 to 7... saturday(6) all day
if (dayOfWeek == 6) {
// successss!!
}
// sunday hour must be lower than 17:42
if (dayOfWeek == 7 && ((hour < 17) || (hour == 17 && minute <= 42)) {
// successss!!
}
A better solution using old Java would look like this:
// current timestamp
GregorianCalendar gcal = new GregorianCalendar();
// specify ISO-week (you are searching for friday until sunday in this order)
gcal.setMinimalDaysInFirstWeek(4);
gcal.setFirstDayOfWeek(Calendar.MONDAY);
// sunday at 17:43
GregorianCalendar sunday = (GregorianCalendar) gcal.clone();
sunday.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
sunday.set(Calendar.HOUR_OF_DAY, 17);
sunday.set(Calendar.MINUTE, 43);
sunday.set(Calendar.SECOND, 0);
sunday.set(Calendar.MILLISECOND, 0);
// friday at 17:42
GregorianCalendar friday = (GregorianCalendar) sunday.clone();
friday.add(Calendar.DATE, -2);
friday.add(Calendar.MINUTE, -1);
// logging for test purposes
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
System.out.println(f.format(friday.getTime()));
System.out.println(f.format(gcal.getTime()));
System.out.println(f.format(sunday.getTime()));
// result (assumption: half-open-interval)
boolean withinTimeWindow = !gcal.before(friday) && gcal.before(sunday);
Java-8 offers a shorter approach (assuming ISO-weekmodel):
LocalDateTime now = LocalDateTime.now();
LocalDateTime friday = now.with(DayOfWeek.FRIDAY).toLocalDate().atTime(17, 42);
LocalDateTime sunday = friday.plusDays(2).plusMinutes(1);
boolean withinTimeWindow = !now.isBefore(friday) && now.isBefore(sunday);
Finally your equivalent evaluation can look like this:
if (!withinTimeWindow) {
if (combined != 0) {
return badge == 1;
} else {
return badge == product;
}
} else {
return badge == 0;
}
Suppose I have a Collection of DateTimes, how I can filter all DateTime objects that have a time between 10h00m and 12h30m?
For example:
new DateTime(2013,1,1,10,0) - is right,
new DateTime(2013,1,1,16,0) - is not.
Parameters like month, year, day are not significant.
Any ideas?
You can really take advantage of joda's LocalTime class here :
LocalTime lowerBound = new LocalTime(10, 0);
LocalTime upperBound = new LocalTime(12, 30);
List<DateTime> filtered = new ArrayList<>();
for (DateTime dateTime : originals) {
LocalTime localTime = new LocalTime(dateTime);
if (lowerBound.isBefore(localTime) && upperBound.isAfter(localTime)) {
filtered.add(dateTime);
}
}
You may need to tweak for inclusive or exclusive, but LocalTime is Comparable, and on top of that, has friendly compare methods that help readability.
List<DateTime> filtered = new ArrayList<>();
for (DateTime dt : mycollection){
if (dt.getHourOfDay >= 10 && dt.getHourOfDay <= 12){
if (dt.getHourOfDay != 12 ||
(dt.getHourOfDay == 12 && dt.getMinuteOfHour <= 30)){
filtered.add(dt);
}
}
}
For simplicity i used Calendar type class.
This should solve your request:
public List<Calendar> filterDateTime(ArrayList<Calendar> dateTimeList) {
List<Calendar> dateTimeFilteredList = new ArrayList<Calendar>();
for (int i=0;i < dateTimeList.size(); i++) {
Calendar currentDateTime = Calendar.getInstance();
currentDateTime.setTime(dateTimeList.get(i).getTime());
// Setting the bottom dateTime value
Calendar filterFrom = Calendar.getInstance();
filterFrom.setTime(currentDateTime.getTime());
filterFrom.set(Calendar.HOUR_OF_DAY, 10);
filterFrom.set(Calendar.MINUTE, 00);
// Setting the upper dateTime value
Calendar filterTo = Calendar.getInstance();
filterTo.setTime(currentDateTime.getTime());
filterTo.set(Calendar.HOUR_OF_DAY, 12);
filterTo.set(Calendar.MINUTE, 30);
if(currentDateTime.after(filterFrom) && currentDateTime.before(filterTo)) {
dateTimeList.add(currentDateTime);
}
}
return dateTimeFilteredList;
}
I want to find difference between 2 Date in months and days using Java. For example: difference between 5/16/2013 and 7/20/2013 is 2 months and 4 days.
Thank you for your consideration on this matter.
Use joda time library as its better to handle dates http://joda-time.sourceforge.net/
Something like this Days.daysBetween(first DateTime, second DateTime).getDays();
I would do it like this
Calendar c1 = new GregorianCalendar(2012, 0, 1);
Calendar c2 = new GregorianCalendar(2013, 0, 2);
int monthDiff = (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
int dayDiff;
if (c1.get(Calendar.DATE) < c2.get(Calendar.DATE)) {
monthDiff--;
dayDiff = c1.getActualMaximum(Calendar.DAY_OF_MONTH) - c1.get(Calendar.DATE) + c2.get(Calendar.DATE);
} else {
dayDiff = c2.get(Calendar.DATE) - c1.get(Calendar.DATE);
}
System.out.println(monthDiff + " " + dayDiff);
Try this one
java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date1 = df.parse("2012-09-30 15:26:14+00");
java.util.Date date2 = df.parse("2012-08-30 15:26:14+00");
int diff = getMonthDifference(date1, date2);
System.out.println(diff);
public static int getMonthDifference(java.util.Date date1, java.util.Date date2) {
if (date1.after(date2)) {
return getMonthDifference0(date1, date2);
} else if (date2.after(date1)) {
return -getMonthDifference0(date2, date1);
}
return 0;
}
private static int getMonthDifference0(java.util.Date date1, java.util.Date date2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
int diff = 0;
while (c2.getTimeInMillis() < c1.getTimeInMillis()) {
c2.add(Calendar.MONTH, 1);
diff++;
}
int dd = c2.get(Calendar.DAY_OF_MONTH) - c1.get(Calendar.DAY_OF_MONTH);
if (dd > 0) {
diff--;
} else if (dd == 0) {
int hd = c2.get(Calendar.HOUR_OF_DAY) - c1.get(Calendar.HOUR_OF_DAY);
if (hd > 0) {
diff++;
} else if (hd == 0) {
long t1 = c1.getTimeInMillis() % (60 * 1000);
long t2 = c2.getTimeInMillis() % (60 * 1000);
if (t2 > t1) {
diff--;
}
}
}
return diff;
}
Nobody said console.log( new Date("2013-09-30") - new Date("2012-01-01") ); It will give you difference in milliseconds. Its up to you to handle time zones and so forth when creating those 2 objects.
I have two dates.Got them from something like......
Calendar c=Calendar.getInstance();
year=c.get(c.YEAR);
month=c.get(c.MONTH);
month++;
date=c.get(c.DATE);
and other date is broken into date2,month2
Now I want to see if both of them are in the same week.
It's possible through lots of calculation and logic.Problem occurs when 1st date is suppose 03 March and 2nd date is 28Feb. Both of them are in same week but difficult to compare/check that. So I want to know if there is any built in function or any way to compare them easily.Please help..........
use something like this:
Calendar c = Calendar.getInstance();
Integer year1 = c.get(c.YEAR);
Integer week1 = c.get(c.WEEK_OF_YEAR);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(/*Second date in millis here*/);
Integer year2 = c.get(c.YEAR);
Integer week2 = c.get(c.WEEK_OF_YEAR);
if(year1 == year2) {
if(week1 == week2) {
//Do what you want here
}
}
This should do it :D
You can get the week number for your date using c.get(Calendar.WEEK_OF_YEAR) and compare the results for your two dates.
Also accessing constants via instance variables (c.YEAR) is not recommended - access them using classes (Calendar.YEAR).
Just posting a slightly modified solution based on #FabianCook and as pointed out by #harshal his solution doesn't cater for two dates on different years but in the same week.
The modification is to actually set the DAY_OF_WEEK in both Calendar dates to point to Monday.....in this case both dates will be set to the same day even if they are in different years and then we can compare them.
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int year1 = cal1.get(Calendar.YEAR);
int week1 = cal1.get(Calendar.WEEK_OF_YEAR);
Calendar cal2 = Calendar.getInstance();
cal2.setTimeInMillis(/*Second date in millis here*/);
cal2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
int year2 = cal2.get(Calendar.YEAR);
int week2 = cal2.get(Calendar.WEEK_OF_YEAR);
if(year1 == year2 && week1 == week2){
//Do what you want here
}
Use java.time
You can get it available the following ways:
Java 8+ and Android API 26+: directly available
Java 6 and 7: ThreeTenBP
Android 25 and lower: ThreeTenABP
It is not recommended to use java.util.Calendar and java.util.Date anymore.
How to find out if two given dates are in the same calendar week?
Using plain java.time:
public boolean inSameCalendarWeek(LocalDate firstDate, LocalDate secondDate) {
// get a reference to the system of calendar weeks in your defaul locale
WeekFields weekFields = WeekFields.of(Locale.getDefault());
// find out the calendar week for each of the dates
int firstDatesCalendarWeek = firstDate.get(weekFields.weekOfWeekBasedYear());
int secondDatesCalendarWeek = secondDate.get(weekFields.weekOfWeekBasedYear());
/*
* find out the week based year, too,
* two dates might be both in a calendar week number 1 for example,
* but in different years
*/
int firstWeekBasedYear = firstDate.get(weekFields.weekBasedYear());
int secondWeekBasedYear = secondDate.get(weekFields.weekBasedYear());
// return if they are equal or not
return firstDatesCalendarWeek == secondDatesCalendarWeek
&& firstWeekBasedYear == secondWeekBasedYear;
}
You can do that with simple int values, too:
public static boolean inSameCalendarWeek(int firstYear, int firstMonth, int firstDayOfMonth,
int secondYear, int secondMonth, int secondDayOfMonth) {
// create LocalDates using the integers provided
LocalDate firstDate = LocalDate.of(firstYear, firstMonth, firstDayOfMonth);
LocalDate secondDate = LocalDate.of(secondYear, secondMonth, secondDayOfMonth);
// get a reference to the system of calendar weeks in your defaul locale
WeekFields weekFields = WeekFields.of(Locale.getDefault());
// find out the calendar week for each of the dates
int firstDatesCalendarWeek = firstDate.get(weekFields.weekOfWeekBasedYear());
int secondDatesCalendarWeek = secondDate.get(weekFields.weekOfWeekBasedYear());
/*
* find out the week based year, too,
* two dates might be both in a calendar week number 1 for example,
* but in different years
*/
int firstWeekBasedYear = firstDate.get(weekFields.weekBasedYear());
int secondWeekBasedYear = secondDate.get(weekFields.weekBasedYear());
// return if they are equal or not
return firstDatesCalendarWeek == secondDatesCalendarWeek
&& firstWeekBasedYear == secondWeekBasedYear;
}
In case you have to extend or use legacy code:
public static boolean inSameCalendarWeek(Calendar firstCalendar, Calendar secondCalendar) {
// create LocalDates from Instants created from the given Calendars
LocalDate firstDate = LocalDate.from(firstCalendar.toInstant());
LocalDate secondDate = LocalDate.from(secondCalendar.toInstant());
// get a reference to the system of calendar weeks in your defaul locale
WeekFields weekFields = WeekFields.of(Locale.getDefault());
// find out the calendar week for each of the dates
int firstDatesCalendarWeek = firstDate.get(weekFields.weekOfWeekBasedYear());
int secondDatesCalendarWeek = secondDate.get(weekFields.weekOfWeekBasedYear());
/*
* find out the week based year, too,
* two dates might be both in a calendar week number 1 for example,
* but in different years
*/
int firstWeekBasedYear = firstDate.get(weekFields.weekBasedYear());
int secondWeekBasedYear = secondDate.get(weekFields.weekBasedYear());
// return if they are equal or not
return firstDatesCalendarWeek == secondDatesCalendarWeek
&& firstWeekBasedYear == secondWeekBasedYear;
}
The output of the following code
public static void main(String[] args) {
// a date from calendar week 1 in the week based year 2020
LocalDate janFirst2020 = LocalDate.of(2020, 1, 1);
// another date from calendar week 1 in the week based year 2020
LocalDate decThirtyFirst2019 = LocalDate.of(2019, 12, 31);
System.out.println(inSameCalendarWeek(janFirst2020, decThirtyFirst2019));
// then a third date from calendar week 1, but this time in the week based year 2021
LocalDate janSeventh2021 = LocalDate.of(2021, 1, 7);
System.out.println(inSameCalendarWeek(janSeventh2021, decThirtyFirst2019));
}
is therefore
true
false
in my locale.
It's a java solution. Following code segment checks if two dates are within same week. It also covers edge cases, where week starts in one calendar year (December) and ends in next year (January).
Note: Code has a dependency on joda-time:
compile 'joda-time:joda-time:2.3'
public static boolean isSameWeek(final Date d1, final Date d2) {
if ((d1 == null) || (d2 == null))
throw new IllegalArgumentException("The date must not be null");
return isSameWeek(new DateTime(d1), new DateTime(d2));
}
public static boolean isSameWeek(final DateTime d1, final DateTime d2) {
if ((d1 == null) || (d2 == null))
throw new IllegalArgumentException("The date must not be null");
// It is important to use week of week year & week year
final int week1 = d1.getWeekOfWeekyear();
final int week2 = d2.getWeekOfWeekyear();
final int year1 = d1.getWeekyear();
final int year2 = d2.getWeekyear();
final int era1 = d1.getEra();
final int era2 = d2.getEra();
// Return true if week, year and era matches
if ((week1 == week2) && (year1 == year2) && (era1 == era2))
return true;
// Return false if none of the conditions are satisfied
return false;
}
Test case:
public class TestDateUtil {
#Test
public void testIsSameWeek() {
final DateTime d1 = new DateTime(2014, 12, 31, 0, 0);
final DateTime d2 = new DateTime(2015, 1, 1, 0, 0);
final DateTime d3 = new DateTime(2015, 1, 2, 0, 0);
final DateTime d4 = new DateTime(2015, 1, 8, 0, 0);
assertTrue(isSameWeek(d1, d2));
assertTrue(isSameWeek(d2, d1));
assertTrue(isSameWeek(d2, d3));
assertTrue(isSameWeek(d3, d2));
assertFalse(isSameWeek(d2, d4));
assertFalse(isSameWeek(d4, d2));
assertFalse(isSameWeek(d1, d4));
assertFalse(isSameWeek(d4, d1));
}
}
I ended up using
date1.with(previousOrSame(MONDAY)).equals(date2.with(previousOrSame(MONDAY)))
assuming that weeks start on Monday.
private int weeksBetween(Calendar startDate, Calendar endDate) {
startDate.set(Calendar.HOUR_OF_DAY, 0);
startDate.set(Calendar.MINUTE, 0);
startDate.set(Calendar.SECOND, 0);
int start = (int)TimeUnit.MILLISECONDS.toDays(
startDate.getTimeInMillis())
- startDate.get(Calendar.DAY_OF_WEEK);
int end = (int)TimeUnit.MILLISECONDS.toDays(
endDate.getTimeInMillis());
return (end - start) / 7;
}
if this method returns 0 they are in the same week
if this method return 1 endDate is the week after startDate
if this method returns -1 endDate is the week before startDate
you get the idea
Let's say, a week starts one year and ends the next year and we pick 2 dates from this week so that they are in different years. Then, the accepted answer yields the wrong result as it checks that the dates are in the SAME YEAR!.
To improve the solution, one could obtain a date corresponding to some day (it could be Monday) of its week and then check that both dates belong to the same day. However, before doing so make sure that both Calendar objects are in the SAME time zone. So, set the time zone of one to another. Also, the solution below works for every API level as it doesn't require the usage of getWeekOfWeekyear() or getWeekYear() (these work with API 24 and higher.
public boolean isSameWeek(Calendar calendar1, Calendar calendar2) {
Calendar calendar2Copy = (Calendar) calendar2.clone();
calendar2Copy.setTimeZone(calendar1.getTimeZone());
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar2Copy.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return calendar1.get(Calendar.YEAR) == calendar2Copy.get(Calendar.YEAR)
&& calendar1.get(Calendar.DAY_OF_YEAR) == calendar2Copy.get(Calendar.DAY_OF_YEAR);
}
this will give true if two dates from same week
fun isEqualWeek(date1: Long, date2: Long):Boolean{
val cal1 = Calendar.getInstance()
val cal2 = Calendar.getInstance()
cal1.time = Date(date1)
cal2.time = Date(date2)
return (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) && (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(
Calendar.WEEK_OF_YEAR
))
}
Here in Joda's Library I found that the week starts from Monday, but I wanted the week to start from Sunday, so just used the below function :
public static boolean isSameWeek(final Date initDate, final Date finalDate) {
if (initDate != null && finalDate != null) {
Calendar initCalender = Calendar.getInstance();
Calendar finalCalender = Calendar.getInstance();
initCalender.setTime(initDate);
finalCalender.setTime(finalDate);
if (finalCalender.get(Calendar.YEAR) >= initCalender.get(Calendar.YEAR)) {
//check for same year
if (finalCalender.get(Calendar.YEAR) == initCalender.get(Calendar.YEAR)) {
if (finalCalender.get(Calendar.WEEK_OF_YEAR) == initCalender.get(Calendar.WEEK_OF_YEAR)) {
return true;
}
} else //check whether next corresponding year
if (finalCalender.get(Calendar.YEAR) - initCalender.get(Calendar.YEAR) == 1) {
if (initCalender.get(Calendar.WEEK_OF_YEAR) == 1 || initCalender.get(Calendar.WEEK_OF_YEAR) == 53) {
if (finalCalender.get(Calendar.WEEK_OF_YEAR) == 1) {
if (initCalender.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && finalCalender.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
return true;
}
}
}
}
} else {
throw new IllegalArgumentException("Final Date should be greater or equal to Initial Date");
}
} else {
throw new IllegalArgumentException("The date must not be null");
}
return false;
}
This question already has answers here:
How do I check if a date is within a certain range?
(17 answers)
Closed 7 years ago.
How can I check if a date is between two other dates, in the case where all three dates are represented by instances of java.util.Date?
This might be a bit more readable:
Date min, max; // assume these are set to something
Date d; // the date in question
return d.after(min) && d.before(max);
If you don't know the order of the min/max values
Date a, b; // assume these are set to something
Date d; // the date in question
return a.compareTo(d) * d.compareTo(b) > 0;
If you want the range to be inclusive
return a.compareTo(d) * d.compareTo(b) >= 0;
You can treat null as unconstrained with
if (a == null) {
return b == null || d.compareTo(b) < 0;
} else if (b == null) {
return a.compareTo(d) < 0;
} else {
return a.compareTo(d) * d.compareTo(b) > 0;
}
Like so:
Date min, max; // assume these are set to something
Date d; // the date in question
return d.compareTo(min) >= 0 && d.compareTo(max) <= 0;
You can use > instead of >= and < instead of <= to exclude the endpoints from the sense of "between."
Between dates Including end points can be written as
public static boolean isDateInBetweenIncludingEndPoints(final Date min, final Date max, final Date date){
return !(date.before(min) || date.after(max));
}
NB: as #Ponmudi pointed out in the comments, this solution may not work if the dates are different at miilliseconds level.
Here's a couple ways to do this using the Joda-Time 2.3 library.
One way is to use the simple isBefore and isAfter methods on DateTime instances. By the way, DateTime in Joda-Time is similar in concept to a java.util.Date (a moment in time on the timeline of the Universe) but includes a time zone.
Another way is to build an Interval in Joda-Time. The contains method tests if a given DateTime occurs within the span of time covered by the Interval. The beginning of the Interval is inclusive, but the endpoint is exclusive. This approach is known as "Half-Open", symbolically [).
See both ways in the following code example.
Convert the java.util.Date instances to Joda-Time DateTime instances. Simply pass the Date instance to constructor of DateTime. In practice you should also pass a specific DateTimeZone object rather than rely on JVM’s default time zone.
DateTime dateTime1 = new DateTime( new java.util.Date() ).minusWeeks( 1 );
DateTime dateTime2 = new DateTime( new java.util.Date() );
DateTime dateTime3 = new DateTime( new java.util.Date() ).plusWeeks( 1 );
Compare by testing for before/after…
boolean is1After2 = dateTime1.isAfter( dateTime2 );
boolean is2Before3 = dateTime2.isBefore( dateTime3 );
boolean is2Between1And3 = ( ( dateTime2.isAfter( dateTime1 ) ) && ( dateTime2.isBefore( dateTime3 ) ) );
Using the Interval approach instead of isAfter/isBefore…
Interval interval = new Interval( dateTime1, dateTime3 );
boolean intervalContainsDateTime2 = interval.contains( dateTime2 );
Dump to console…
System.out.println( "DateTimes: " + dateTime1 + " " + dateTime1 + " " + dateTime1 );
System.out.println( "is1After2 " + is1After2 );
System.out.println( "is2Before3 " + is2Before3 );
System.out.println( "is2Between1And3 " + is2Between1And3 );
System.out.println( "intervalContainsDateTime2 " + intervalContainsDateTime2 );
When run…
DateTimes: 2014-01-22T20:26:14.955-08:00 2014-01-22T20:26:14.955-08:00 2014-01-22T20:26:14.955-08:00
is1After2 false
is2Before3 true
is2Between1And3 true
intervalContainsDateTime2 true
Another option
min.getTime() <= d.getTime() && d.getTime() <= max.getTime()
Here you go:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String oeStartDateStr = "04/01/";
String oeEndDateStr = "11/14/";
Calendar cal = Calendar.getInstance();
Integer year = cal.get(Calendar.YEAR);
oeStartDateStr = oeStartDateStr.concat(year.toString());
oeEndDateStr = oeEndDateStr.concat(year.toString());
Date startDate = sdf.parse(oeStartDateStr);
Date endDate = sdf.parse(oeEndDateStr);
Date d = new Date();
String currDt = sdf.format(d);
if((d.after(startDate) && (d.before(endDate))) || (currDt.equals(sdf.format(startDate)) ||currDt.equals(sdf.format(endDate)))){
System.out.println("Date is between 1st april to 14th nov...");
}
else{
System.out.println("Date is not between 1st april to 14th nov...");
}
}
You might want to take a look at Joda Time which is a really good API for dealing with date/time. Even though if you don't really need it for the solution to your current question it is bound to save you pain in the future.
import java.util.Date;
public class IsDateBetween {
public static void main (String[] args) {
IsDateBetween idb=new IsDateBetween("12/05/2010"); // passing your Date
}
public IsDateBetween(String dd) {
long from=Date.parse("01/01/2000"); // From some date
long to=Date.parse("12/12/2010"); // To Some Date
long check=Date.parse(dd);
int x=0;
if((check-from)>0 && (to-check)>0)
{
x=1;
}
System.out.println ("From Date is greater Than ToDate : "+x);
}
}
you can use getTime() and compare the returned long UTC values.
EDIT if you are sure you'll not have to deal with dates before 1970, not sure how it will behave in that case.
Here's how to find whether today is between 2 months:
private boolean isTodayBetween(int from, int to) {
if (from < 0 || to < 0 || from > Calendar.DECEMBER || to > Calendar.DECEMBER) {
throw new IllegalArgumentException("Invalid month provided: from = " + from + " to = " + to);
}
Date now = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(now);
int thisMonth = cal.get(Calendar.MONTH);
if (from > to) {
to = to + Calendar.DECEMBER;
thisMonth = thisMonth + Calendar.DECEMBER;
}
if (thisMonth >= from && thisMonth <= to) {
return true;
}
return false;
}
and call it like:
isTodayBetween(Calendar.OCTOBER, Calendar.MARCH)