zoukankan      html  css  js  c++  java
  • 较复杂的日期时间处理

    1、如何如何将一个字符串如“ 20110826134106”装化为任意的日期时间格式,下面列举两种类型:
    NSString* string = @"20110826134106";
    NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    [inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSDate* inputDate = [inputFormatter dateFromString:string];
    NSLog(@"date = %@", inputDate);
    
    NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [outputFormatter setLocale:[NSLocale currentLocale]];
    [outputFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
    NSString *str = [outputFormatter stringFromDate:inputDate];
    NSLog(@"testDate:%@", str);
    两次打印的结果为:
    date = 2011-08-26 05:41:06 +0000
    testDate:2011年08月26日 13时41分06秒
    /*
    
    NSDateFormatter *dateFormatter=[[NSDateFormatteralloc]init];
    [dateFormatter setDateFormat:@"yyyy"];
    NSString *Str=[dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release];
    NSDateFormatter *dateFormatter1=[[NSDateFormatteralloc]init];
    [dateFormatter1 setDateFormat:@"MM"];
    NSString *Str1=[dateFormatter1 stringFromDate:[NSDate date]];
    [dateFormatter1 release];
    NSDateFormatter *dateFormatter2=[[NSDateFormatteralloc]init];
    [dateFormatter2 setDateFormat:@"dd"];
    NSString *Str3=[dateFormatter2 stringFromDate:[NSDate date]];
    
    [dateFormatter2 release];
    NSString *dateStr=[NSStringstringWithFormat:@"%@-%@-%@ 00:00:00",Str,Str1,Str3];
    NSLog(@"dateStr------------%@",dateStr);
    //将传入时间转换成需要的格式
    NSDateFormatter *format=[[NSDateFormatteralloc]init];
    //[dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];//location设置为中国
    
    // [format setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];//location设置为中国
    [format setLocale:[NSLocalecurrentLocale]];
    
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *inputDate=[[format dateFromString:dateStr] retain];
    NSTimeZone *fromzone = [NSTimeZonesystemTimeZone];
    NSInteger frominterval = [fromzonesecondsFromGMTForDate: inputDate];
    fromDate = [[inputDate dateByAddingTimeInterval: frominterval] retain];
    
    NSLog(@"fromDate------zh---------%@",fromDate);
    
    - (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
    //默认日期格式为yyyy-MM-dd
    dateFormatter= [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];//location设置为中国
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //picker的默认时间为当前时间
    date=[NSDate date];
    //picker的默认style为只显示日期
    datePickerMode=UIDatePickerModeDate;
    
    //构造一个子视图,用于显示日期选择器
    subview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    subview.backgroundColor=[UIColor clearColor];
    subview.tag=0;
    
    */
    
    说明:上面的时间是美国时间,下面的没有设置
    
    NSString* string = @"Wed, 05 May 2011 10:50:00 +0800";
    NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
    [inputFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
    NSDate* inputDate = [inputFormatter dateFromString:string];
    NSLog(@"date = %@", inputDate);
    
    // 获得本地时间指定时区
    NSDate *dates = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
    [formatter setTimeZone:timeZone];
    NSString *loctime = [formatter stringFromDate:dates];
  • 相关阅读:
    set命令_Linux
    AngularJS的date 过滤器
    JMeter环境介绍
    JMeter测试计划要素
    HTTP协议的压缩及URL Encode
    fiddler配置及使用教程
    Slenium常用方法
    Selenium八大定位
    CSS实现上下左右垂直居中
    SASS用法笔记
  • 原文地址:https://www.cnblogs.com/fs-ios/p/4994175.html
Copyright © 2011-2022 走看看