• 判断当前时间是否在一个时间段范围内


    + (BOOL)isBetweenFromHour:(NSInteger)fromHour toHour:(NSInteger)toHour
    {
        NSDate *dateFromHour = [self getCustomDateWithHour:fromHour];
        NSDate *dateToHour = [self getCustomDateWithHour:toHour];
        
        NSDate *currentDate = [NSDate date];
        
        if ([currentDate compare:dateFromHour]==NSOrderedDescending && [currentDate compare:dateToHour]==NSOrderedAscending)
        {
            return YES;
        }
        return NO;
    }
    
    
    + (NSDate *)getCustomDateWithHour:(NSInteger)hour
    {
        //获取当前时间
        NSDate *currentDate = [NSDate date];
        NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *currentComps = [[NSDateComponents alloc] init];
        
        NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
        
        currentComps = [currentCalendar components:unitFlags fromDate:currentDate];
        
        //设置当天的某个点
        NSDateComponents *resultComps = [[NSDateComponents alloc] init];
        [resultComps setYear:[currentComps year]];
        [resultComps setMonth:[currentComps month]];
        [resultComps setDay:[currentComps day]];
        [resultComps setHour:hour];
        
        NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        return [resultCalendar dateFromComponents:resultComps];
    }
  • 相关阅读:
    myeclipse 配置svn
    windows下 将tomcat做成服务,并于oracle后启动
    局部内部类为什么只能访问final局部变量,对于成员变量却可以随便访问?
    使用cmd查看windows端口占用情况,并关闭应用
    生成javadoc文档
    JNI以及JNA使用
    自定义标签-java
    dwr框架应用
    Hadoop生态圈简介
    tomcat之日志记录
  • 原文地址:https://www.cnblogs.com/joesen/p/4173630.html
Copyright © 2020-2023  润新知