Kyaw Zin Wai
Kyaw Zin Wai

Reputation: 459

NSCalendar currentCalendar not working

In my project. I want user changes calendar in my project. if user choose Japanese Calendar, then the date show with Japanese Calendar format in my project. If user choose Gregorian, then the date show with Gregorian Calendar format. So i used the class file as below.

Here is myclass.h

#import <Foundation/Foundation.h>

@interface util : NSObject

+ (NSString *)getTrimedString:(NSString *)str;

+(NSDate*)convertStringToDate:(NSString*)baseString;
+(NSString*)dateToYMD:(NSDate*)date;
+(NSString*)dateToMDHM:(NSDate*)date;
+(NSString*)dateToYMDHM:(NSDate*)date;

+(NSString*)dateToHM:(NSDate*)date;
+(NSString*)convertStringToMDHM:(NSString*)baseString;

+(UILabel*)createWorkLblWord:(NSString*)baseText;
+(UILabel*)createWorkLblWord:(NSString*)baseText widths:(NSInteger)width fontSize:(NSInteger)size;

+(float)systemVersionFloat;

@end


+(NSString*)dateToYMDHM:(NSDate*)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

Here is myclass.m

#import "util.h"

@implementation util

+(NSString*)dateToYMDHM:(NSDate*)date{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];



NSCalendar *usercalendar = [NSCalendar currentCalendar];

[formatter setCalendar:usercalendar];

NSLog(@"Current Auto Calendar : %@",[[NSCalendar autoupdatingCurrentCalendar] calendarIdentifier]);
NSLog(@"current calendar: %@", [[NSCalendar currentCalendar] calendarIdentifier]);

[formatter setDateFormat:@"yyyy/MM/dd\nH:mm"];
    return [formatter stringFromDate:date];
}

But the result is only show Gregorian Calendar Date format. Even user choose Japanese Calendar or Buddhist Calendar, my project only show Gregorian Calendar Format Date. How can I solve this?

Here is NSLog Screenshot

Upvotes: 2

Views: 1253

Answers (3)

Kyaw Zin Wai
Kyaw Zin Wai

Reputation: 459

The solved answer for my question is not Coding error. Its just wrong in Scheme Options. I changed my Application Region to System Region in Xcode Product Menu >> Scheme >> Edit Scheme >> Options Tab. Mr.Andrew Romanov saved me. Thanks a lot Mr.Andrw Romanov

Here is my Scheme Options Screenshot.

enter image description here

Upvotes: 1

Andrew Romanov
Andrew Romanov

Reputation: 5066

currentCalendar it is calendar from current locale, that user can change in the phone's settings.
If you want to allow to user changes the calendar in the application. You shoud create needed calendar with calendarWithIdentifier method.
Also you can subscribe to NSCurrentLocaleDidChangeNotification, and update the UI.

All code here:

@interface ViewController ()

- (IBAction)printCurrentCalendar:(id)sender;
- (void)localeChangedNotification:(NSNotification*)notification;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localeChangedNotification:) name:NSCurrentLocaleDidChangeNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark Actions
- (IBAction)printCurrentCalendar:(id)sender
{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSLog(@"calendar from currentCalendar: %@", calendar.calendarIdentifier);

    NSCalendar* localeCalendar = [NSCalendar calendarWithIdentifier:[NSLocale currentLocale].calendarIdentifier];
    NSLog(@"calendar from locale: %@",  localeCalendar.calendarIdentifier);
}


- (void)localeChangedNotification:(NSNotification*)notification
{
    NSLog(@"locale changed");
}

@end

output:

CalendarCheck[1289:74644] calendar from currentCalendar: gregorian CalendarCheck[1289:74644] calendar from locale: gregorian
CalendarCheck[1289:74644] locale changed
CalendarCheck[1289:74644] calendar from currentCalendar: japanese CalendarCheck[1289:74644] calendar from locale: japanese

Upvotes: 0

Misha
Misha

Reputation: 685

instead of using [NSCalendar currentCalendar] try using identifiers, current Calendar picks the default one

for Gregorian [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

for buddhist `[[NSCalendar alloc]initWithCalendarIdentifier: NSBuddhistCalendar];

Upvotes: 0

Related Questions