无论日期如何,检查给定时间是否介于两次之间

时间:2013-07-17 10:55:15

标签: java android date calendar timespan

我有时间跨度:

  

String time1 = 01:00:00

     

String time2 = 05:00:00

我想检查 time1 time2 是否都位于20:11:13 and 14:49:00之间。

实际上,01:00:00大于20:11:13且小于14:49:00,因为20:11:13始终小于14:49:00。这是先决条件。

所以我想要的是20:11:13 < 01:00:00 < 14:49:00

所以我需要这样的东西:

 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;
    }
 }

我知道这个代码没有给出正确的结果,因为我在比较字符串对象。

如何做到这一点,因为它们是时间跨度而不是要比较的字符串?

28 个答案:

答案 0 :(得分:43)

您可以使用Calendar课程进行检查。

例如:

try {
    String string1 = "20:11:13";
    Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(time1);

    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();
}

答案 1 :(得分:20)

TL;博士

  

20:11:13&lt; 01:00:00&lt;十四点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

java.time类包括LocalTime来表示只有没有日期且没有时区的时间。

  

所以我想要的是,20:11:13&lt; 01:00:00&lt;十四点49分00秒。

首先我们定义边界。您的输入字符串恰好符合标准ISO 8601格式。默认情况下,java.time类使用ISO 8601格式,因此无需指定格式化模式。

LocalTime start = LocalTime.parse( "20:11:13" );
LocalTime stop = LocalTime.parse( "14:49:00" );

定义我们的测试用例,即目标01:00:00

LocalTime target = LocalTime.parse( "01:00:00" );

现在我们设置比较这些LocalTime个对象。我们想看看目标是在较晚时间之后但在较早时间之前。这意味着在这种情况下半夜,第二天早上大约晚上8点到凌晨3点之间。

Boolean isTargetAfterStartAndBeforeStop = ( target.isAfter( start ) && target.isBefore( stop ) ) ;

该测试可以更简单地说明为“ not 在凌晨3点到晚上8点之间”。然后我们可以推广到任何一对LocalTime对象,我们在之间测试如果开始在24小时制停止之前,不在之间如果在停止之后开始(如本问题的话)。

此外,时间跨度通常采用半开放式方法处理,其中开头是包含,而结尾是独占。所以&#34;之间&#34;严格地说,比较是“目标是等于或晚于开始并且目标是在停止之前”,或者更简单地说,“目标不是在开始之前和停止之前”。

Boolean isBetweenStartAndStopStrictlySpeaking = 
    ( ( ! target.isBefore( start ) && target.isBefore( stop ) ) ;

如果开始是在停止之后,在24小时内,则假设我们需要问题中建议的逻辑(在晚上8点之后但在凌晨3点之前)。

if( start.isAfter( stop ) ) {
    return ! isBetweenStartAndStopStrictlySpeaking ;
} else {
    return isBetweenStartAndStopStrictlySpeaking ;
}

关于 java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和&amp; SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

答案 2 :(得分:18)

@kocko给出的答案仅在同一天起作用 如果开始时间“23:00:00”并结束“02:00:00” [第二天],当前时间为“01:30:00 “然后结果将是假的...
我修改了 @ kocko的答案以完美地运作

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");
        }
    }
}

输出

"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 

(我已将下限值包括在[上限值-1])

答案 3 :(得分:14)

修改@Surendra Jnawali&#39;码。它失败了

如果当前时间是23:40:00,即大于开始时间且小于等于23:59:59。

所有信用都归于真正的所有者

这应该是这样的:这很完美

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

答案 4 :(得分:10)

 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);
    }
 }

更进一步,更准确地说, 如果比较当天00:00到24:00之间的时间间隔, 你也需要解析这一天。

答案 5 :(得分:2)

这里有很多答案,但是我想提供一个与Basil Bourque's answer类似但有完整代码示例的新答案。因此,请参见以下方法:

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!");
    }
}

如果您使用以下方法调用此方法:

checkTime("20:11:13", "14:49:00", "01:00:00");

或使用:

checkTime("20:11:13", "14:49:00", "05:00:00");

结果将是:

  

介于两者之间!

答案 6 :(得分:2)

看完一些回复后,我觉得写作太复杂了。试试我的代码

 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;
 } 

答案 7 :(得分:2)

使用 LocalTime 只会忽略日期值:

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);
        } 

}

答案 8 :(得分:2)

听起来我的问题是OR情况......你想检查一下是否时间1&gt; 20:11:13或者时间1&lt;十四点49分00秒。

在20:11:13之间永远不会超过你的范围到另一端(14:49:00),反之亦然。可以把它想象成一个时间不在正确排序的时间戳之间。

答案 9 :(得分:2)

以下方法检查'validateTime'是否在'startTime'和&amp;之间。 'endTime'与否,同时考虑'endTime'可能是第二天的可能性。要正确使用它,请在“HH:mm”共振峰中解析日期。

//module declaration
var app = angular.module('myApp',[]);
//controller declaration
app.controller('myCtrl',function($scope, $injector){
    var a = $injector.get('myFactory')
    console.log(a.country);
});

//factory declaration
app.factory('myFactory',function(){
    var obj = {};
    obj.country = "USA";
    return obj;
});

答案 10 :(得分:1)

抱歉sudo代码..我在打电话。 ;)

between = (time < string2 && time > string1);
if (string1 > string2)  between = !between;

如果它们是时间戳或字符串,则可行。只需更改变量名称即可匹配

答案 11 :(得分:1)

我是这样做的:

LocalTime time = LocalTime.now();
if (time.isAfter(LocalTime.of(02, 00)) && (time.isBefore(LocalTime.of(04, 00))))
{
    log.info("Checking after 2AM, before 4AM!");                    
}

编辑:

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");  
}    

答案 12 :(得分:1)

在下面的代码snipet中,正在验证是否在开始和结束时间之间存在当前时间(可以是任何时间):

        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
        }

答案 13 :(得分:1)

Java 8 - LocalDateTime

这个怎么样?

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
}

答案 14 :(得分:1)

在您的情况下,开始时间(20:11:13)大于结束时间(14:49:00)。您可以通过在结束时间添加一天或从开始时间减去一天来解决问题,这是一个合理的假设。如果你这样做,你将被困,因为你不知道测试时间是哪一天。

您可以通过检查测试时间是否在结束时间和开始时间之间来避免此陷阱。如果是,那么结果是&#34;而不是在#34 ;;否则结果就是&#34;介于&#34;之间。

这是我一直在使用的JAVA中的功能。它对我来说到目前为止。祝你好运。

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;
    }
}

要创建日历实例,您可以使用:

Calendar startC = Calendar.getInstance();
startC.set(Calendar.HOUR_OF_DAY, 20);
startC.set(Calendar.MINUTE,11);
startC.set(Calendar.SECOND,13);

答案 15 :(得分:1)

实际工作功能如下

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;
}

答案 16 :(得分:1)

在@kocko的帮助下,完整的工作代码如下:

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();
}

答案 17 :(得分:0)

从逻辑上讲,如果你做了以下事情,那么你应该总是可以使用军事时间......

如果开始时间大于结束时间,则将结束时间加24 否则按原样使用次数

比较当前时间在开始和结束时间之间。

答案 18 :(得分:0)

许多人注意到,这不是日期问题,而是逻辑问题。 假设一天分为两个间隔:一个间隔在20:11:13和14:49:00之间,而另一个间隔在14:49:00和20:11:13之间(极端属于哪个间隔您)。 如果您想查看某个感兴趣的时间20:11:13/14:49:00中是否包含某个时间,只需检查另一时间14:49:00 /中是否包含了该时间20:11:13,因为数字的自然顺序,然后取反,会容易得多。

答案 19 :(得分:0)

从$ time中剥离冒号,从字符串中取$和$,转换为int,然后使用以下条件检查时间是否在from和to之间。示例是在php中,但不应该重要。

if(($to < $from && ($time >= $from || $time <= $to)) ||
    ($time >= $from && $time <= $to)) {
    return true;
}

答案 20 :(得分:0)

这里是使用新的Java 8类的解决方案,结构紧凑,不需要正则表达式或手动算术运算。我的解决方案针对包括开始时间和排除结束时间进行了编码,但是可以根据您的需要轻松进行修改。

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");
 }

答案 21 :(得分:0)

基于此处大多数作者的想法和解决方案,我想与大概更简洁的代码分享我的完善解决方案:

    /**
 * 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;
}

为了完整起见,我添加了A.3和A.4的条件,但是在高效的代码中,您可以省略它。

现在,您可以简单地创建开始日期和结束日期,以及您要检查并调用此静态方法的时间。代码如下:

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
}

对于TDD方面,我已经为上述场景A和B添加了单元测试。如果您发现任何错误或需要优化的地方,请随时检查并报告。

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));
    }
}

干杯!

答案 22 :(得分:0)

/**
 * @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");
    }
}

答案 23 :(得分:0)

针对所有差距的简单解决方案:

 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);

}

答案 24 :(得分:0)

这对我有用:

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
    }
}

答案 25 :(得分:0)

基于 Konstantin_Yovkov answer,我想分享我的实现,它检查当前时间是否在给定的 START 和 END 时间之间。

这个实现假设如果给定的 END 时间在 START 时间之前,那么 END 必须是明天:

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;
    }
}

答案 26 :(得分:0)

用 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
        }
    }

格式化程序只需要 HH:mm:ss,所以它与日期无关。所有日期都计算为 1970 年 1 月 1 日作为纪元开始日期。因此,时间的比较只发生在时间,因为这里所有案例的日期都是 1970 年 1 月 1 日。

注意:使用旧版 Java API 而不是较新的 API(LocalTime 和 DateTimeFormatter),因为这些较新的 API 在旧设备(如 Oreo 版本以下的 Android)上不受支持。如果您使用其他平台可以获得更新的 API,请使用它们,因为它们更优化且错误更少。

答案 27 :(得分:-2)

这很有效,但需要一些改进。

long timeToSeconds(String time){
    long ret=0;
    String[] ar = time.split("\\:");
    for(int i=0;i < ar.length ; i++){
        ret+= Long.valueOf(ar[i])* Math.pow(60,(2-i)); // (60^(2-i));
    }
    return ret;
}
boolean isTimeBetween(String startTime, String endTime,String currentTime) {
    long lCurrentTime = timeToSeconds(currentTime);
    long lstartTime = timeToSeconds(startTime);
    long lEndTime = timeToSeconds(endTime);

    if(((lstartTime-lCurrentTime)*(lEndTime-lCurrentTime)*(lstartTime-lEndTime))>0){
        return true;
    }else{
        return false;
    }
}

像这样修改你的代码 -

 public void getTimeSpans(){
    boolean firstTime = false, secondTime = false;

    if(isTimeBetween("20:11:13" ,"14:49:00",time1)
    {
        firstTime = true;
    }

    if(isTimeBetween("20:11:13" ,"14:49:00",time2)
    {
        secondTime = true;
    }
}