zoukankan      html  css  js  c++  java
  • NSDate 时间戳与字符串转换

    1. 一,转化的方法为  
    2.     NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[localeDate timeIntervalSince1970]];  
    3.     NSLog(@"timeSp:%@",timeSp); //时间戳的值  
    4.   
    5. 二,把获取的时间转化为当前时间  
    6.  NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式  
    7.     NSTimeZone *zone = [NSTimeZone systemTimeZone];  
    8.     NSInteger interval = [zone secondsFromGMTForDate:datenow];  
    9.     NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];  
    10.     NSLog(@"%@", localeDate);  
    11.   
    12. 3.把时间戳转化为时间的方法  
    13.     NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1363948516];  
    14.     NSLog(@"1363948516  = %@",confromTimesp);  
    15.   
    16. //timer  
    17.     NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式  
    18.     NSTimeZone *zone = [NSTimeZone systemTimeZone];  
    19.     NSInteger interval = [zone secondsFromGMTForDate:datenow];  
    20.     NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];  
    21.     NSLog(@"%@", localeDate);  
    22.       
    23.     NSString *timeSp = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]];  
    24.     NSLog(@"timeSp:%@",timeSp); //时间戳的值 1369189763711   1369218563 1369218614  
    25.       
    26.     NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1369189763711/1000];  
    27.     NSLog(@"1363948516  = %@",confromTimesp);  
    28.       
    29.     //实例化一个NSDateFormatter对象  
    30.   
    31.     //判断昨天 前几天等 判断今天凌晨时间戳  
    32.     NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];  
    33.     [dateFormatter1 setDateFormat:@"yyyy-MM-dd 00:00:00"];  
    34.     NSString *currentDateStr1 = [dateFormatter1 stringFromDate:[NSDate date]];  
    35.     NSLog(@"凌晨时间:%@",currentDateStr1);  
    36.     NSString *timeSp1 = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]];  
    37.     NSLog(@"凌晨时间戳:%@",timeSp1);  
    38.     //昨天凌晨时间戳  
    39.     NSString *timeSp2 = [NSString stringWithFormat:@"%lld", (long long)[localeDate timeIntervalSince1970]-24*60*60];  
    40.     NSLog(@"昨天凌晨时间戳:%@",timeSp2);  
  • 相关阅读:
    创建线程的方式三:实现Callable接口 --- JDK 5.0新增
    线程的通信
    多线程的实例练习:银行账户双储户问题
    解决线程安全问题的方式三:Lock锁 --- JDK5.0新增
    演示线程的死锁问题
    Synchronized的各场景使用方法(多窗口售票例子接上篇)
    线程的【生命周期】和【线程的同步】(下面有多窗口售票例子)
    多线程:继承方式和实现方式的联系与区别
    创建多线程的方式二:实现Runnable接口
    Java项目生成可执行jar包、exe文件以及在Windows下的安装文件
  • 原文地址:https://www.cnblogs.com/fanjing/p/4568467.html
Copyright © 2011-2022 走看看