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

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

  • 相关阅读:
    Linux 文件及目录管理命令基础
    MHA高可用及读写分离
    MySQL的备份和回复
    mysql的主从复制
    MySQL索引管理及执行计划
    [LeetCode]Linked List Cycle II解法学习
    浅谈reverse_iterator的base()函数
    [LeetCode]LRU Cache有个问题,求大神解答【已解决】
    分享一篇不错的博文《写给准备参加秋招的学弟学妹们~一定要来看哦~》
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4020954.html
Copyright © 2011-2022 走看看