便利--获取系统的时分秒,年月日

在项目开发的时候我们经常会使用到时间,如何快速获得现在的时间:

源代码:

LGlobalNormalTime.h 与 LGlobalNormalTime.m

 1 //  Created by Lisa on 14-10-14.
 2 //  Copyright (c) 2014年 Lisa. All rights reserved.
 3 //
 4
 5 #import <Foundation/Foundation.h>
 6
 7 @interface LGlobalNormalTime : NSObject
 8
 9
10 /*
11  当前时间的数组
12  @return 返回有3个元素的数组(0处小时、1处位分钟、2处为秒)
13  */
14 +(NSArray *)currentTime;
15
16 /*
17  当前秒
18  @return 当前秒
19  */
20
21 +(float)currentSecond;
22
23 /*
24  当前分钟
25  @return 当前分钟
26  */
27 +(float)currentMinute;
28
29 /*
30  当前小时
31  @return 当前小时
32  */
33 +(float)currentHours;
34
35
36 /*
37  当前年份
38  @return 当前年份
39  */
40 +(CGFloat)currentYear;
41
42 /*
43  当前月份
44  @return 当前年份
45  */
46 +(CGFloat)currentMonth;
47
48 /*
49  当前day
50  @return 当前day
51  */
52 +(CGFloat)currentDay;
53
54 @end
  1 //  Created by Lisa on 14-10-14.
  2 //  Copyright (c) 2014年 Lisa. All rights reserved.
  3 //
  4
  5 #import "LGlobalNormalTime.h"
  6
  7 static NSDateFormatter* _DMLogdateFormatter= nil;
  8 static NSDateFormatter* _DMYearDateFormatter= nil;
  9
 10
 11 @implementation LGlobalNormalTime
 12
 13 +(void)initialize
 14 {
 15     if (self == [LGlobalNormalTime class]) {
 16         //日期格式
 17         _DMLogdateFormatter = [[NSDateFormatter alloc]init];
 18         [_DMLogdateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]];
 19         [_DMLogdateFormatter setDateFormat:@"HH:mm:ss"];
 20
 21         _DMYearDateFormatter = [[NSDateFormatter alloc]init];
 22         [_DMYearDateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"]];
 23         [_DMYearDateFormatter setDateFormat:@"yyyy-MM-dd"];
 24     }
 25 }
 26
 27
 28 /*
 29  当前时间的数组
 30  @return 返回有3个元素的数组(0处小时、1处位分钟、2处为秒)
 31  */
 32 +(NSArray *)currentTime
 33 {
 34     NSString *timeNow = [_DMLogdateFormatter stringFromDate:[NSDate date]];
 35     NSArray *timeArray = [timeNow componentsSeparatedByString:@":"];
 36     NSString *yearNow = [_DMYearDateFormatter stringFromDate:[NSDate date]];
 37     NSArray *yearArray = [yearNow componentsSeparatedByString:@"-"];
 38     NSMutableArray *allArray = [NSMutableArray arrayWithCapacity:1];
 39     [allArray addObjectsFromArray:timeArray];
 40     [allArray addObjectsFromArray:yearArray];
 41
 42     return allArray;
 43
 44 }
 45
 46 /*
 47  当前秒
 48  @return 当前秒
 49  */
 50
 51 +(float)currentSecond
 52 {
 53     NSArray *timeArray = [self currentTime];
 54     float second  = [timeArray[2] intValue];
 55     return second;
 56 }
 57
 58 /*
 59  当前分钟
 60  @return 当前分钟
 61  */
 62 +(float)currentMinute
 63 {
 64     NSArray *timeArray = [self currentTime];
 65     float minute  = [timeArray[1] intValue];
 66     return minute;
 67 }
 68
 69 /*
 70  当前小时
 71  @return 当前小时
 72  */
 73 +(float)currentHours
 74 {
 75     NSArray *timeArray = [self currentTime];
 76     float hour  = [timeArray[0] intValue];
 77     return hour;
 78 }
 79
 80
 81
 82 /*
 83  当前年份
 84  @return 当前年份
 85  */
 86 +(CGFloat)currentYear
 87 {
 88     NSArray *timeArray = [self currentTime];
 89     float year  = [timeArray[3] intValue];
 90     return year;
 91 }
 92 /*
 93  当前月份
 94  @return 当前年份
 95  */
 96 +(CGFloat)currentMonth
 97 {
 98     NSArray *timeArray = [self currentTime];
 99     float month  = [timeArray[4] intValue];
100     return month;
101 }
102 /*
103  当前day
104  @return 当前day
105  */
106 +(CGFloat)currentDay
107 {
108     NSArray *timeArray = [self currentTime];
109     float day  = [timeArray[5] intValue];
110     return day;
111 }
112
113
114
115 @end

代码示例:

打印输出结果:

时间: 2024-08-01 22:46:42

便利--获取系统的时分秒,年月日的相关文章

便利的获取系统的时分秒

源码如下: 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 GlobalNorma

JS获取系统的指定定年月日

1 /** 2 * 获取系统当前时间 3 */ 4 function getNowYearMouth(){ 5 var date=new Date; 6 var nowYearMouth=date.getMonth()+1; 7 var year=date.getFullYear(); 8 var day = date.getDate(); 9 nowYearMouth =(nowYearMouth<10 ? "0"+nowYearMouth:nowYearMouth); //获

Sql 获取当前日期没有时分秒

select convert(varchar(10),getdate(),120) 输出格式:2008-02-27 00:25:13 SELECT CONVERT(char(19), getdate(), 120) 输出格式:2008-02-27 SELECT CONVERT(char(10), getdate(), 12) 输出格式:2008.02.27 SELECT CONVERT(char(10), getdate(), 102) 输出格式:08.02.27 SELECT CONVERT(

java 获取系统变量(环境变量和设置变量)

前言 环境变量这个概念不陌生, 就是操作系统的环境变量. 系统变量就是java本身维护的变量. 通过 System.getProperty 的方式获取. 对于不同的操作系统来说, 环境变量的处理可能会有一些不统一的地方, 比方说: 不区分大写和小写 等等. Java 获取环境变量 Java 获取环境变量的方式非常easy: System.getEnv()  得到全部的环境变量 System.getEnv(key) 得到某个环境变量的值 Map map = System.getenv(); Ite

sql server获取当前年月日 时分秒

获取当前年月日(字符串): select CONVERT(varchar(11),GETDATE(),112) 获取当前时间的时分秒(':'隔开): select CONVERT(varchar(12),GETDATE(),108) 将年月日时分秒拼接成一条字符串: select CONVERT(varchar(11),GETDATE(),112)+REPLACE(CONVERT(varchar(12),GETDATE(),108),':','')

SqlSever基础 getdate函数 返回系统当前的年月日,时分秒 毫秒

镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 1 code 1 --返回系统当前的年月日,时分秒 毫秒 2 select getdate() 2 show ------------------------------------------博文的精髓,在技术部分,更在镇场一诗.SqlSever是优秀的语言,值得努力学习.熟悉数据库的增删查改,

获取年月日时分秒

NSDate *now = [NSDate date]; NSCalendar *cal = [NSCalendar currentCalendar]; unsigned int unitFlags = NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dd = [cal components:unitFlags fromDate:

获取当前年月日...信息的方法,获取请求的ip地址,由于数据库中是time格式,所以要获取时分秒的格式

1.获取当前年月日...信息的方法 1 public class CommonDate { 2 3 public final String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)); 4 public final String month = String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+1); 5 public final String

java Date获取 年月日时分秒

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.util; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; public class Test {     public vo