zoukankan      html  css  js  c++  java
  • 便利的获取系统的时分秒

    便利的获取系统的时分秒

    源码如下:

    GlobalNormalTime.h 与 GlobalNormalTime.m

    //
    //  GlobalNormalTime.h
    //  YouXianMingClock
    //
    //  Created by YouXianMing on 14-10-12.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface GlobalNormalTime : NSObject
    
    /**
     *  当前时间的数组
     *
     *  @return 返回有3个元素的数组(0处为小时,1处为分钟,2处为秒)
     */
    + (NSArray *)currentTime;
    
    /**
     *  当前秒
     *
     *  @return 当前秒
     */
    + (float)currentSecond;
    
    /**
     *  当前分钟
     *
     *  @return 当前分钟
     */
    + (float)currentMinute;
    
    /**
     *  当前小时
     *
     *  @return 当前小时
     */
    + (float)currentHour;
    
    @end
    //
    //  GlobalNormalTime.m
    //  YouXianMingClock
    //
    //  Created by YouXianMing on 14-10-12.
    //  Copyright (c) 2014年 YouXianMing. All rights reserved.
    //
    
    #import "GlobalNormalTime.h"
    
    static NSDateFormatter* _DMLogDateFormatter = nil;
    
    @implementation GlobalNormalTime
    
    + (void)initialize
    {
        if (self == [GlobalNormalTime class]) {
            // 日期格式
            _DMLogDateFormatter = [[NSDateFormatter alloc] init];
            [_DMLogDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
            [_DMLogDateFormatter setDateFormat:@"HH:mm:ss"];
        }
    }
    
    + (NSArray *)currentTime
    {
        NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
        NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
        return timeArray;
    }
    
    + (float)currentSecond
    {
        NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
        NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
        
        // 获取到时间
        float sec =  [timeArray[2] intValue];
        return sec;
    }
    
    + (float)currentMinute
    {
        NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
        NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
        
        // 获取到时间
        float min =  [timeArray[1] intValue];
        return min;
    }
    
    + (float)currentHour
    {
        NSString *timerNow = [_DMLogDateFormatter stringFromDate:[NSDate date]];
        NSArray *timeArray = [timerNow componentsSeparatedByString:@":"];
        
        // 获取到时间
        float hour =  [timeArray[0] intValue];
        return hour;
    }
    
    @end

    以下提供简易分析,使用非常简单,试一下就知道了

  • 相关阅读:
    OC 中的强引用(strong referene)和弱引用( weak reference)
    OC 内存管理黄金法则
    理解NSSComparisionResult 类型
    Java 持有对象学习笔记
    Java相关配置
    Flash Builder
    WPS 导致 EXCEL 文件下载问题
    Cocos2dx 3.8-----裁剪层中控件的点击响应问题
    #cocos2dx游戏开发#UIImageView必须在加载了图片资源后再进行九宫格设置,否则无效。
    在普通class里使用onActivityResult获取从一个activity返回的数据 (待解)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4020954.html
Copyright © 2011-2022 走看看