투케이2K

131. (Objective-C/objc) NSDateFormatter 사용해 date 포맷 형식 지정 및 string to date 형변환 수행 실시 본문

Objective-C

131. (Objective-C/objc) NSDateFormatter 사용해 date 포맷 형식 지정 및 string to date 형변환 수행 실시

투케이2K 2022. 11. 1. 16:44
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

// MARK: - [헤더 파일에 정의 없이 : void 메소드 구현]
- (void)testMain {
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> testMain() :: 테스트 메소드 수행] \n");
    printf("==================================== \n");
    printf("\n");

    
    // [try catch 구문 정의 실시]
    @try {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            // [string to date 변환 실시]
            NSString *dateString =  @"2022-11-01 16:30:45";
            
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            NSDate *date = [dateFormatter dateFromString:dateString];
            
            
            // [date to string 변환 실시]
            NSString *convertDate = [dateFormatter stringFromDate:date];

            
            // [로그 출력 실시]
            NSLog(@"==================================== \n");
            NSLog(@"[ViewController >> testMain() :: NSLog 로그 결과 확인 실시] \n");
            NSLog(@"==================================== \n");
            NSLog(@"[date :: %@] \n", date);
            NSLog(@"==================================== \n");
            NSLog(@"[convertDate :: %@] \n", convertDate);
            NSLog(@"==================================== \n");

        });

    }
    @catch (NSException *exception) {
        printf("\n");
        printf("==================================== \n");
        printf("[ViewController >> catch :: 예외 상황 확인] \n");
        printf("[name :: %s] \n", exception.name.description.UTF8String);
        printf("[reason :: %s] \n", exception.reason.description.UTF8String);
        printf("==================================== \n");
        printf("\n");
    }
}
 

[결과 출력]


 

반응형
Comments