首页 > 代码库 > NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用

NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用

看来很多网上关于日期的处理,今天、昨天、前天、后天、一周等,满心欢喜的拿着去验证结果总是不尽如人意,看别别人的代码看的脑涨,自己就写一个简单的,自己来用,以后用其他的方法,我会在完善,再次记录,以方便以后查阅

                                                                                                                                    --宋飞祥    2014.7.19

直接上代码,很容易理解:

#import <Foundation/Foundation.h>

@interface MyCutomNSDateDeal : NSObject

+(NSInteger)timeZoneOffset;
+(NSInteger)preDayOfTheDate_start:(NSDate *)date;
+(NSInteger)preDayOfTheDate_end:(NSDate *)date;
+(NSInteger)nextDayOfTheDate_start:(NSDate *)date;
+(NSInteger)nextDayOfTheDate_end:(NSDate *)date;

@end

#import "MyCutomNSDateDeal.h"

@implementation MyCutomNSDateDeal

+(NSInteger)timeZoneOffset
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger offset = [zone secondsFromGMTForDate:date];
    return offset;
}
/**
 *  根据给出的日期,返回该日期的前一天的起点(相对于日期起点,seconds的计数)
 *
 *  @param date 日期
 *
 *  @return 返回的是seconds计数,相对于日历系统起始至该日期前一天的起点(就是0点)
 */
+(NSInteger)preDayOfTheDate_start:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]-24];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}

/**
 *  根据给出的日期,返回该日期的前一天的终点(也就是给出日期的起点)
 *
 *  @param date 给定的日期
 *
 *  @return 返回的是seconds计数
 */
+(NSInteger)preDayOfTheDate_end:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
}

+(NSInteger)nextDayOfTheDate_start:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]+24];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
    
}

+(NSInteger)nextDayOfTheDate_end:(NSDate *)date
{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit|NSSecondCalendarUnit) fromDate:date];
    [components setHour:-[components hour]+ 2*24];
    [components setMinute:-[components minute]];
    [components setSecond:-[components second]];
    NSDate *date1 = [cal dateByAddingComponents:components toDate:date options:0];
    
    return [date1 timeIntervalSince1970]+[self timeZoneOffset];
    
}

//+(NSInteger)preWeekOfTheDate_

@end


使用如下:

这里我取得date 为当天的,用户可以换成自己想要的相对日期即可!!

 NSDate *today = [NSDate date];
    NSLog(@"today ::::: %@",today);
    NSInteger today_int = [MyCutomNSDateDeal preDayOfTheDate_start:today];
    NSDate *per_start = [NSDate dateWithTimeIntervalSince1970:today_int];
    NSLog(@"per_start _:_ %@",per_start);
    NSDate *per_end = [NSDate dateWithTimeIntervalSince1970:[MyCutomNSDateDeal preDayOfTheDate_end:today]];
    NSLog(@"per_end -:- %@",per_end);
    NSDate *next_start = [NSDate dateWithTimeIntervalSince1970:[MyCutomNSDateDeal nextDayOfTheDate_start:today]];
    NSDate *next_end = [NSDate dateWithTimeIntervalSince1970:[MyCutomNSDateDeal nextDayOfTheDate_end:today]];
    NSLog(@"next_start === %@",next_start);
    NSLog(@"next_end === %@",next_end);
打印结果如下:

2014-07-19 <span style="color:#FF0000;">15:26:30.565 </span>FMDBSortByDateDemo[4423:60b] today ::::: 2014-07-19 <span style="color:#FF0000;">07:26:30</span> +0000
2014-07-19 15:26:30.566 FMDBSortByDateDemo[4423:60b] per_start _:_ 2014-07-18 00:00:00 +0000
2014-07-19 15:26:30.566 FMDBSortByDateDemo[4423:60b] per_end -:- 2014-07-19 00:00:00 +0000
2014-07-19 15:26:30.566 FMDBSortByDateDemo[4423:60b] next_start === 2014-07-20 00:00:00 +0000
2014-07-19 15:26:30.567 FMDBSortByDateDemo[4423:60b] next_end === 2014-07-21 00:00:00 +0000
第一行打印可以看出,我们打印的时间理我现在真正需要的时间差8个小时,这就是时区的偏移,加上8个小时就对了,下面的打印在我定义的头文件中,已经把相对偏加上了!


这里我想说的是:我项目中用到要存数据和date,而数据库(FMDB)是不支持date格式的,我采取的是把相对日历起点的seconds(秒)存起来,通过比对秒数来取想要的数据!

如果大家有好的方法,可以交流!!

下面是我的数据库取值过程:

 NSInteger inter = [MyCutomNSDateDeal preDayOfTheDate_end:today];
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Temperature WHERE Date > %i ",inter];
   // NSString *sql = [NSString stringWithFormat:@"SELECT * FROM Temperature WHERE Date between %li and %li",from,to];

    if ([db open]) {
       FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSString *name = [rs stringForColumn:@"Name"];
            NSInteger time = [rs intForColumn:@"Date"];
            NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
            float temp = [rs doubleForColumn:@"Temperature"];
            NSString *event = [rs stringForColumn:@"Event"];
            NSLog(@"Name:%@,Date:%@,Temperature:%f,Event:%@",name,date,temp,event);
        }
    }

两个sql语句都验证了,是正确的,有一点我要说的是,我们在处理取出的数据时,加上了偏移量,所以我们存取数据时,也要加上偏移量,应该如下:

-(void)temperatureAndDateUpdate:(float)value
{
    NSDate *date = [NSDate date];
    NSInteger time = [date timeIntervalSince1970];
    time = time + [MyCutomNSDateDeal timeZoneOffset]; //加上时区偏移量
    NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:time];
    NSLog(@"temperature update    date1 === %@",date1);
    if ([db open]) {
        
        [db executeUpdate:@"INSERT INTO Temperature(Name,Date,Temperature,Event) VALUES (?,?,?,?)",@"SFX",[NSNumber numberWithInteger:time],[NSNumber numberWithFloat:value],@"吃药"];
        NSLog(@"success!!");
    }else
    {
       NSLog(@"error:   %@   %@",[db lastError],[db lastErrorMessage]);
    }
    [db close];
}


NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用