获取当前时间---年月日时分秒------iOS

本文介绍了两种在Objective-C中获取当前日期时间并将其格式化为年-月-日 时:分:秒的方法。第一种方法使用NSDateFormatter设置特定的日期时间样式,并直接将当前时间格式化为指定格式。第二种方法通过NSDateComponents获取各时间元素,再进行字符串转换及拼接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方式一:XXXX年-XX月-XX日  XX时:XX分:XX秒的格式

- (IBAction)LoginAction:(UIButton *)sender

{

   
    NSDate *date = [NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *DateTime = [formatter stringFromDate:date];
    NSLog(@"%@============年-月-日  时:分:秒=====================",DateTime);

}

  方式二:XXXX年-X月-X日  XX时:XX分:XX秒的格式  
- (IBAction)LoginAction:(UIButton *)sender{

  NSDate *now = [NSDate date];
    NSLog(@"now date is: %@", now);
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int year =(int) [dateComponent year];
    int month = (int) [dateComponent month];
    int day = (int) [dateComponent day];
    int hour = (int) [dateComponent hour];
    int minute = (int) [dateComponent minute];
    int second = (int) [dateComponent second];
    
    NSInteger year = [dateComponent year];
    NSInteger month =  [dateComponent month];
    NSInteger day = [dateComponent day];
    NSInteger hour =  [dateComponent hour];
    NSInteger minute =  [dateComponent minute];
    NSInteger second = [dateComponent second];

    
    NSLog(@"year is: %ld", (long)year);
    NSLog(@"month is: %ld", (long)month);
    NSLog(@"day is: %ld", (long)day);
    NSLog(@" hour is: %ld",  (long)hour);
    NSLog(@"minute  is: %ld", (long)minute );
    NSLog(@"second is: %ld", (long)second);
     //字符串的转化并且拼接
      NSString *yearstr=[NSString stringWithFormat:@"%ld-",(long)year];
      NSString *monthstr=[NSString stringWithFormat:@"%ld-",(long)month];
     NSString *daystr=[NSString stringWithFormat:@"%ld ",(long)day];
     NSString *hourstr=[NSString stringWithFormat:@"%ld:",(long)hour];
    NSString *minutestr=[NSString stringWithFormat:@"%ld:",(long)minute];
    NSString *secondstr=[NSString stringWithFormat:@"%ld",(long)second];
    //字符串开始拼接
    NSString *allstr=[yearstr stringByAppendingString:monthstr];
    NSString *allstr1=[allstr stringByAppendingString:daystr];
    NSString *allstr2=[allstr1 stringByAppendingString:hourstr];
    NSString *allstr3=[allstr2 stringByAppendingString:minutestr];
    NSString *DateTime=[allstr3 stringByAppendingString:secondstr];
    NSLog(@"最后年月日时分秒拼接的结果=====%@",DateTime);
}

    

转载于:https://www.cnblogs.com/dongiosblogs/p/4612749.html

1 按照某种格式输出系统时间:自学java中的时间。自学Date类(java.util.Date)和SimpleDateFormat类,用程序输出“当前时间是XXXX年XX月XX日XX时XX分XX秒(第XX周,周X)” 2 比较String与StringBuffer、StringBuilder的自增速度。方法:在自增前记录系统时间,进行5000次自增,然后再记录系统时间,可以得出自增的速度。获取系统时间的方法是java.lang.System.currentTimeMillis() ,见JDK API。 3 定义点类,圆是由圆心(点)和半径组成的,请完成圆类,注意“派”为常量,取3.14。圆类具有judge函数可以判断两圆的关系:相离、相交(包括相切)、嵌套、重合。设计static成员用以求所有圆对象的平均面积。(static、final) 4 图形间的关系可以下图用来表现。所有的图形中都可以称为Shape。由这个类可以派生出二维图形TwoDimensionalShape和三维图形ThreeDimensionalShape类。每个TwoDimensionalShape类应包括成员函数getArea以计算二维图形的面积。每个ThreeDimensionalShape类包含成员函数getArea和getVolume,分别计算三维图形的表面积和体积。编写一个程序,用一个数组乘放各种图形类对象(包括二维和三维的),并输出对象得相应信息。如果某个图形是TwoDimensionalShape就显示其面积,如果某个图形是ThreeDimenionalShape,则显示其面积和体积。请完成图中所有的类并在主函数中做测试。(继承与多态)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值