1

I can get milliseconds in Java with:

Date date=new Date() ;  
System.out.println("Today is " +date.getTime());

How do I get milliseconds in objective C? I have to have the same result for both code.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
leonvian
  • 4,719
  • 2
  • 26
  • 31
  • 1
    http://stackoverflow.com/questions/889380/how-can-i-get-a-precise-time-for-example-in-milliseconds-in-objective-c – Perception Apr 12 '13 at 23:25
  • Did you try searching before asking? In the Related area, I found this one as second question: http://stackoverflow.com/questions/3990295/in-iphoneobjective-c-how-do-i-get-the-current-date-formatted-in-miliseconds-to?rq=1 – Darwind Apr 12 '13 at 23:25
  • Since ObjC is C, you should just be able to use `time()*1000` from `time.h`. – millimoose Apr 12 '13 at 23:25
  • (And honestly, if you need a predictable time representation, go with a timezone-aware ISO8601 one.) – millimoose Apr 12 '13 at 23:26

1 Answers1

3

The documentation for Java Date.getTime() is:

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

For Objective-C the analogous method is:

- (NSTimeInterval)timeIntervalSince1970

Return Value: The interval between the receiver and the reference date, 1 January 1970, GMT.

An NSTimeInterval is a double in seconds. So, this does it:

  llrint (1000.0 * [date timeIntervalSince1970]);

to get you a long (as Java produces).

Note: 'duplicate' answers do not produce a long as the Java interface does; they produce a double.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • 1
    +1 This directly answers the question of a Java-compatible ms count. Note that `llrint` may be more accurate than `lrint` because a Java long is always 64 bits. – Roger Feb 19 '16 at 15:27