zoukankan      html  css  js  c++  java
  • iOS获取网络时间与转换格式

     

    [NSDate date]可以获取系统时间,但是会造成一个问题,用户可以自己修改手机系统时间,所以有时候需要用网络时间而不用系统时间。获取网络标准时间的方法:

    1、先在需要的地方实现下面的代码,创建一个URL并且连接

    1 NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
    2     NSURLRequest *request=[NSURLRequest requestWithURL:url];
    3     NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
    4     [connection start];

    2、实现代理方法,接收回应的数据(需要声明<NSURLConnectionDataDelegate>)

    /**
     *  代理方法
     */
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    //      NSLog(@"response--%@",response);
            NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
        if ([response respondsToSelector:@selector(allHeaderFields)]) {
            NSDictionary *dict=[httpResponse allHeaderFields];
    //      NSLog(@"dict--%@",dict);
            NSString *time=[dict objectForKey:@"Date"];
            NSLog(@"date--%@___class---%@",date,[date class]);
        }
    }
    

     这时候接收到的数据是这样的

    Tue, 30 Jun 2015 03:55:54 GMT

    类型是NSCFString,如果我们需要用的是NSDate的数据就需要进行转换。。。。。。

    转换的方法是。。。。。。。。。。。你需要了解的知识是截取字符串,格式化......具体做法是这样的,可以写一个转换的函数,把字符串作为参数传进来,1、截取字符串

    NSString *timeStr=[str substringWithRange:NSMakeRange(5, 20)];//截取字符串

    2、格式化

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

    [formatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];//设置源时间字符串的格式

    NSDate* date = [formatter dateFromString:timeStr];//将源时间字符串转化为NSDate

    在模拟器上运行的时候,你会发现出来的数据比标准时间还少了8小时。。。。。。Why????

    少了这个

    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

    [formatter setTimeZone:timeZone];

    好,以为做完了,模拟器上也正常了,真机试试,出来的值为null.....好烦...其实还少了配置区域

    是这样:

    NSLocale *local=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];

    [formatter setLocale:local];//需要配置区域,不然会造成模拟器正常,真机日期为null的情况

    en_US_POSIX是什么?查了一下,还有另外一个选择,是en_US,两个有什么区别呢?目前换了另一个参数看上去结果是一样的,但是苹果推荐用en_US_POSIX,文档上说的意思大约是,en_US_POSIX和en_US出来的时间是一样的,但是如果美国,在未来的某个时刻,它改变日期格式的方式,用“en_US”将改变以反映新的行为,但“en_US_POSIX”不会,在机器上(“en_US_POSIX”适用于iPhone操作系统一样,它在Mac OS X上。

    最后付上转换代码:

    -(NSDate *)dateFromNSString:(NSString *)str{
        //Tue, 30 Jun 2015 03:55:54 GMT
        //30 Jun 2015 03:55:54
         NSString *timeStr=[str substringWithRange:NSMakeRange(5, 20)];//截取字符串
         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
         NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
         NSLocale *local=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
         [formatter setLocale:local];//需要配置区域,不然会造成模拟器正常,真机日期为null的情况
         [formatter setTimeZone:timeZone];
         [formatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];//设置源时间字符串的格式
          NSDate* date = [formatter dateFromString:timeStr];//将源时间字符串转化为NSDate
          NSLog(@"date--%@___class---%@",date,[date class]);
        //可以自己再换格式,上面是源,下面是目标  
    //     NSDateFormatter* toformatter = [[NSDateFormatter alloc] init];
    //     [toformatter setDateStyle:NSDateFormatterMediumStyle];
    //     [toformatter setTimeStyle:NSDateFormatterShortStyle];
    //     [toformatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];//设置目标时间字符串的格式
    //     NSString *targetTime = [toformatter stringFromDate:date];//将时间转化成目标时间字符串
    //     NSDate* toDate = [formatter dateFromString:targetTime];//将源时间字符串转化为NSDate
          return date;
    }
    
  • 相关阅读:
    LINUX 环境变量总结
    make的自动变量和预定义变量
    函数调用约定和堆栈
    如何查看linux命令源代码
    shell脚本中特定符合变量的含义
    【转载】Redhat5和6 YUM源配置的区别
    用路径分析法来编写测试用例
    linux ip 设置
    Mysql 的存储引擎,myisam和innodb的区别。
    一些编译php时的configure 参数
  • 原文地址:https://www.cnblogs.com/tcan/p/4611269.html
Copyright © 2011-2022 走看看