我的iOS学习历程 - OC第七天

今天主要讲的是NSDate的使用(各种方法的使用) 还有类目的使用

最重要的是协议的使用步骤(由于代码的类不好写上来看 请点击下载源码),在后面点击下载(点击进入下载页面)


(main中使用的类目代码在最下层)

系统内表示时间日期的类 NSDate 

NSDate *date = [NSDatedate];

打印出来的是格林威治时间0时区

咱们在东八区 



距离现在 n秒之后的后期   

NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:24 *3600];
距离 2001-1-1 00.00.00 n 秒之后的时间    
NSDate *date2 = [NSDate dateWithTimeIntervalSinceReferenceDate:3600];NSLog(@"%@",date2);
距离 1970-1-1 00:00:00 n 秒之后的时间   
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:3600];
    练习题:计算当前时间和⼀个固定时间的差值,如果差值在 60 秒内,输出 ,如果在 60 秒外 3600 秒内,输出 “xx 分钟前 ,如果 3600 秒外,3600*24 秒内,输出 “xx ⼩时前

   

NSDate *date = [NSDate date];

NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:3000];

NSTimeInterval interval = [date1timeIntervalSinceDate:date];

NSLog(@"%.0f",interval);

if (interval <=60 ) {

        NSLog(@"刚刚");

}elseif (interval <=3600){

        NSLog(@"%.0f分钟前",interval /60);

}elseif(interval >3600 *24){

        NSLog(@"%.0f小时前",interval /3600);

}
   

按你喜欢的格式来输出时间

    继承一个抽象类

    抽象类的特点:抽象类本身不实现什么具体功能

    具体的功能由其子类去实现  

 NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];

// 添加一个格式

//  2015年 11月11日 20-00-00

// 先创建一个NSDateFormatter类型数据用setDateFormat改变格式创建一个时间然后使用NSDateFormatter中的stringFromDate方法转化为字符串格式输出

[dateformat setDateFormat:@"yyyy年 MM月dd日 HH-mm-ss"];

// 把当前时间转化为按上面的格式输出

    NSDate *date = [NSDate date];

    NSString *str = [dateformat stringFromDate:date ];

    NSLog(@"%@",str);
   

   把一个日期时间的字符串转化为NSDate打印出来

NSDateFormatter *dateformat1 = [[NSDateFormatter alloc]init];

NSString *str1 =@"2012.04.01 20:21:56";

[dateformat1 setDateFormat:@"yyyy.MM.dd HH:mm:ss"];

// 创建一个时区

NSTimeZone *zone1 = [NSTimeZone  timeZoneForSecondsFromGMT:0];

// 设置一下时区

[dateformat1 setTimeZone:zone1];

NSDate *date1 = [dateformat1 dateFromString:str1];

NSLog(@"%@",date1);
   

       时区类   

NSTimeZone *zone = [NSTimeZone systemTimeZone];

定义⼀个NSDateFormatter,设置合适的格式。将字符串@“20140501102318转换为NSDate对象。  

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒"];

NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0];

[dateFormatter setTimeZone:zone];

NSString *str =@"2014年05月01日 10点23分18秒";

NSDate *date = [dateFormatter dateFromString:str];

NSLog(@"%@",date);
  

    /* ***********************************类的扩展******************************** */

    

    类目

特点:可以给看不到实现的系统类添加方法并且添加的方法使用系统类的对象或者类名直接就可以调用

切记:

1.只能添加方法不能添加实例变量

2.类目中添加的方法相当于直接添加到系统类中是可以被继承的

 

   [NSString printfEnergy];

// 使用NSString的子类调用类目中的方法

[NSMutable String printfEnergy];

    // 练习: 使⽤Category为NSDate类添加⼀个类⽅法(dateWithDateString:),实现将下述字符串转换为NSDate对象。将@“20140402142850”转换为NSDate。

NSString *str =@"20140402142850";

[NSDate dateWithDateString:str];


                                                    以下是类目的代码


NSDate+DateBecomeString.h

#import <Foundation/Foundation.h>

@interface NSDate (DateBecomeString)
+(void)dateWithDateString:(NSString *)DateString;
@end
NSDate+DateBecomeString.m
#import "NSDate+DateBecomeString.h"

@implementation NSDate (DateBecomeString)
+(void)dateWithDateString:(NSString *)DateString{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0];

    [format setDateFormat:@"yyyyMMddHHmmss"];
    [format setTimeZone:zone];
    NSDate *date = [format dateFromString:DateString];
    NSLog(@"%@",date);
}
@end

NSString+PrintfString.h

<span style="font-size:18px;color:#333333;">#import <Foundation/Foundation.h>

@interface NSString (PrintfString)

//  给字符串添加一个类方法
//  打印你的潜力有多大
+(void)printfEnergy;</span>

NSString+PrintfString.m


@implementation NSString (PrintfString)
+(void)printfEnergy{
    NSLog(@"你的潜力有多大");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值