zoukankan      html  css  js  c++  java
  • 获得当前的系统时间和日期

    1。获得当前的系统时间和日期

     //获得系统时间  

    NSDate *  senddate=[NSDate date];  

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

    [dateformatter setDateFormat:@"HH:mm"]; 

    NSString *  locationString=[dateformatter stringFromDate:senddate];

     //[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];  

    //NSString *  morelocationString=[dateformatter stringFromDate:senddate];  

    //获得系统日期  

    NSCalendar  * cal=[NSCalendar  currentCalendar];  

    NSUInteger  unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;  

    NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];  

    NSInteger year=[conponent year];  

    NSInteger month=[conponent month];  

    NSInteger day=[conponent day];  

    NSString *  nsDateString= [NSString  stringWithFormat:@"%4d年%2d月%2d日",year,month,day];  

    [dateformatter release];  

    2。从字符串来获得NSDate

    string  strYear="1988";  

    string  strMonth="09";  

    string  strDay="18";  

    string  strHour="5";  

    string  strMinutes="18";  

    string  strSec="20";  

    morelocationString=[NSString stringWithFormat:@"%s-%s-%s-%s-%s-%s",strYear.c_str(),strMonth.c_str(),  

                                    strDay.c_str(),strHour.c_str(),strMinutes.c_str(),strSec.c_str()];  

    //根据时间字符串获得NSDate  

             

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

      [dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];  

      NSDate  * oldDate=[dateformatter dateFromString:morelocationString];  

       通过上面的代码,获得了NSDate。

    3。 从GMT时间,得到本地时间

      NSDate  * oldDate=[dateformatter dateFromString:morelocationString];   

      NSTimeInterval  timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];

        NSDate  * newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];  

  • 相关阅读:
    spring配置初始化出错
    Java常用工具类(计算MD5,验证码随机生成,天数差值计算)
    Java基础(静态static)
    websocket使用nginx代理后连接频繁打开和关闭
    关于kafka客户端版本与服务端版本不一致导致的一次坑
    kafka
    maven常用命令含义
    pg数据库org.postgresql.util.PSQLException: ERROR: "xxx" is not a sequence
    @Param注解和@Mapper注解
    springmvc对参数接收的两个注解@RequestParam和@RequestBody
  • 原文地址:https://www.cnblogs.com/496668219long/p/4983764.html
Copyright © 2011-2022 走看看