iOS前期OC训练OC_07NSDate

//

//  main.m

//  OC07_NSDate

//

//  Created by dllo on 15/7/23.

//  Copyright (c) 2015 Clare. All rights reserved.

//


#import <Foundation/Foundation.h>


int main(int argc, const char * argv[]) {

//    NSArray *arr = @[@"1", @"2", @"3", @"4"];

//    NSDictionary *dic = @{@"1" : @"2", @"3" : @"4"};

//    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"2", @"1", @"4", @"3",@"6", @"5", nil];

//    // 迭代器

//    // 数组

//    NSEnumerator *enumerator1 = [arr objectEnumerator];

//    NSString *str = nil;

//    while (str = [enumerator1 nextObject]) {

//        NSLog(@"%@", str);

//    }

//

//    // 字典

//    // 打印字典的value

//    NSEnumerator *enumer = [dic objectEnumerator];

//    NSString  *str1 = nil;

//    while (str1 = [enumer nextObject]) {

//        NSLog(@"%@", str1);

//    }

//    

//    NSEnumerator *enumer1 = [dic2 objectEnumerator];

//    NSString  *str3 = nil;

//    while (str3 = [enumer1 nextObject]) {

//        NSLog(@"%@", str3);

//    }

    

    

    // 值对象

    // 把基本数据类型转换成对象类型

    // Interger -> NSNumber

//    NSInteger a = 10;

//    NSNumber *number = [[NSNumber alloc] initWithInteger:a];

//    NSLog(@"%@", number);

//    // NSNumber -> Integer

//    NSInteger b = [number integerValue];

//    // 字面量

//    NSNumber *num = @10;

//    

//    NSArray *arr1 = @[@"1", @10];

    

    

    // NSValue

    // 把结构体和指针换成对象类型

    // 结构体的,是访问成员变量,对象调用属性是点语法

//    NSRange range = {2, 5};

//    NSLog(@"%ld", range.length);

//    

//    // NSRange -> NSValue

//    NSValue *value = [NSValue valueWithRange:range];

//    NSLog(@"%@", value);

//    

//    // NSValue -> NSRange

//    NSRange rangeV = [value rangeValue];

//

//    int a = 10;

//    int *p = &a;

//    NSValue *pointer = [NSValue valueWithPointer:p];

//    

//    int *p1 = [pointer pointerValue];

    

    

    // 文件路径

//    NSString *path = @"/Users/dllo/Desktop/Clare/OC/OC07_NSDate/OC07_NSDate/movielist.txt";

//    

//    // 转换成NSDate对象

//    NSData *data = [NSData dataWithContentsOfFile:path];

//    

//    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

//    NSLog(@"%@", dic);

//    

//    NSMutableArray *arr = dic[@"result"];

//    NSLog(@"%@", arr);

//    

//    NSDictionary *movieDic = arr[0];

//    NSLog(@"%@", movieDic);

//    

//    NSString *str = movieDic[@"movieName"];

//    NSLog(@"%@", str);

//    

//    NSString *movieName = dic[@"result"][1][@"movieName"];

//    NSLog(@"%@", movieName);

//    

//    NSString *picUrl = dic [@"result"][0][@"pic_url"];

//    NSLog(@"%@", picUrl);

//    

//    // 打印所有的电影名

//    NSArray *arr = dic[@"result"];

//    for (NSDictionary *dic in arr) {

//        NSLog(@"%@", dic[@"movieName"]);

//    }

//    for (NSDictionary *dic in dic) {

//        NSArray *result = dic[@"result"];

//        for (NSArray *arr in result) {

//            NSMutableDictionary *moviename = arr[@"movieName"];

//            for (NSString *name in movieName)

//                NSLog(@"%@", name);

//            }

//        }

//    }

//    

    

//    NSString *pathOne = @"/Users/dllo/Desktop/Clare/OC/OC07_NSDate/OC07_NSDate/activitylist.txt";

//    NSData *dataOne = [NSData dataWithContentsOfFile:pathOne];

//    

//    NSMutableDictionary *dicOne = [NSJSONSerialization JSONObjectWithData:dataOne options:0 error:nil];

//    NSLog(@"%@", dicOne);

    NSString *zhaolei = dicOne[@"events"][2][@"owner"][@"name"];

    NSLog(@"%@", zhaolei);

//    

//    // 先找到开始时间

//    NSString *begTime = dicOne[@"events"][2][@"begin_time"];

//    

//    // 结束时间

//    NSString *endTime = dicOne[@"events"][2][@"end_time"];

//    NSString *newTime = [   [  [ begTime substringWithRange:NSMakeRange(0,begTime.length - 3 ) ] stringByAppendingString:@"-"  ]stringByAppendingString :[ endTime substringWithRange:NSMakeRange(endTime.length - 8, 5) ]   ];

//    NSLog(@"%@", newTime);

//    

//    NSString *newEnd = [endTime substringFromIndex:endTime.length - 8];

//    NSLog(@"%@", newEnd);

//    NSString *newTime1 = [NSString stringWithFormat:@"%@-%@", begTime, newEnd];

//    NSLog(@"%@", newTime1);

    

    //NSDictionary *dic = dicOne[@"events"];

//    NSArray *arr = dicOne[@"events"];

//    NSInteger num = 0;

//    for (NSDictionary *dic in arr) {

//        NSString *cate = dic[@"category"];

//        if ([cate isEqualToString:@"film"]) {

//            num++;

//        }

//    }

//    NSLog(@"%ld", num);


    

    // NSSet集合

    // NSSet是无序的

    // NSSet不能有重复的元素

    // 集合里的元素,必须是对象类型

//    NSSet *set = [NSSet setWithObjects:@"1", @"2", @"3", @"4", @"5",@"2", nil];

//    NSLog(@"%@", set);

//    NSLog(@"%ld", set.count);

//    // 返回其中的某一个元素

//    NSLog(@"%@", [set anyObject]);

//    

//    NSLog(@"%d", [set containsObject:@"3"]);

    

//    NSSet *set = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];

//    NSLog(@"%@", set);

    

//    NSMutableSet *set = [NSMutableSet setWithObjects:@"1", @"2", @"3", @"4", @"5", nil];

//    NSLog(@"%@", set);

    

//    // 添加元素

//    [set addObject:@"6"];

//    NSLog(@"%@", set);

//    // 删除

//    [set removeObject:@"2"];

//    NSLog(@"%@", set);

    //

    // forin进行遍历

//    for (NSString *str in set) {

//        NSLog(@"%@", str);

//    }

    

//    // NSDate是一个日期的类

//    NSDate *date = [NSDate date];

//    NSLog(@"%@", date);

//    // +date获取的时间无论在哪个区,都是打印对应的零时区的时间

//    

//    // 获取一下当前所在的时区

//    NSTimeZone *zone = [NSTimeZone systemTimeZone];

//    NSLog(@"%@", zone);

//    

//    // 获取一下和0时区相差的秒数

//    NSInteger seconds = [zone secondsFromGMTForDate:date];

//    NSLog(@"%ld", seconds);

//    

//    // 通过相差的秒数,能获取到现在的时间

//    NSDate *localDate = [NSDate dateWithTimeIntervalSinceNow:seconds];

//    NSLog(@"%@", localDate);

//    

//    NSDate *times = [NSDate dateWithTimeIntervalSince1970:0];

//    NSLog(@"%@", times);

//    

//    // 一个明天这个时候的时间

//    NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSinceNow:(32 * 3600)];

//    NSLog(@"%@", tomorrowDate);

//    // 昨天这个时候的时间

//    NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:(32 * 3600 * (- 1))];

//    NSLog(@"%@", yesterdayDate);

//    

//    // 时间间隔

//    NSTimeInterval interval = [tomorrowDate timeIntervalSinceDate:date];

//    NSLog(@"%g", interval);

//    

//    // 计算当前时间和一个固定时间的差值,如果差值在60秒内,输出刚刚”,如果在60秒外3600秒内,输出“xx分钟前”,如果3600秒外, 3600*24秒内,输出“xx⼩小时前

//    NSDate *fixtime = [NSDate date];

//    NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:-1000];

//    NSInteger seconds1 = [fixtime timeIntervalSinceDate:date2];

//    if (seconds1 < 60) {

//        NSLog(@"刚刚");

//    } else if (seconds1 >= 60 && seconds1 <3600) {

//        NSInteger minute = seconds1 / 60;

//        NSLog(@"%ld分钟前", minute);

//    } else if (seconds1 >= 3600 && seconds1 < 3600 *24                                                                                                                                                                                                                                                                                                                                                 ){

//        NSInteger hour = seconds1 / 60 / 60;

//        NSLog(@"%ld小时前", hour);

//    }

    

    

    // 把日期和字符串的一个互相的转换

    // NSDate -> NSString

    // 第一种方式

//    NSDate *date = [NSDate date];

//    NSString *dateStr = [NSString stringWithFormat:@"%@", date];

//    NSLog(@"%@", dateStr);

    

    // 时间的格式

    // yyyy-MM-dd HH-mm-ss

    // y

    // M

    // d

    // H 24小时制, h 12xiaos

    // m       s

    

    // 先设置一下时间的格式, 要转换的时间要和格式相吻合

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

    

    [formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];

//    NSDate *date = [NSDate date];

//    // 通过格式,把指定的时间直接转换成NSString

//    // 通过这种方式,系统还会把时间切换成当前的时间

//    NSString *strDate = [formatter stringFromDate:date];

//    NSLog(@"%@", strDate);

    

    // 字符串 -> NSDate

    // 把时间又减掉8小时

    NSString *timeStr = @"2015-7-23 17-18-10";

    NSDate *date = [formatter dateFromString:timeStr];

    NSLog(@"%@", date);

    

    

    return 0;

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值