Whats the best way to get the current time (HH:MM:SS) using Objective-C. I would guess I should be looking at NSDate & NSDateFormatter. I had a quick glance at the docs and it looked a little more convoluted than I expected, so I thought I would check here to make sure I was on the right track.
gary
To retrieve the current system date and time in local time, use the GetLocalTime function.
Here is the code. . . . public static void getCurrentTimeUsingCalendar() { Calendar cal = Calendar. getInstance(); Date date=cal. getTime(); DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String formattedDate=dateFormat.
You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.
NSDate * now = [NSDate date]; NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; [outputFormatter setDateFormat:@"HH:mm:ss"]; NSString *newDateString = [outputFormatter stringFromDate:now]; NSLog(@"newDateString %@", newDateString); [outputFormatter release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With