0

I use j2objc to translate a java util Date to a objective-c JavaUtilDate class. I imported #import "java/util/Date.h"to store a translated java Date variable.

var myDate: JavaUtilDate

How do I convert a JavaUtilDate into an NSDate?

Michael
  • 32,527
  • 49
  • 210
  • 370
  • So what is a JavaUtilDate? Tell us that and somebody can help you. – Duncan C Jun 19 '15 at 16:20
  • @DuncanC Don't down vote this is a serious question. I use: JavaUtilDate from https://github.com/google/j2objc – Michael Jun 19 '15 at 16:37
  • @confile Please edit your Question with additional info rather than posting comments. – Basil Bourque Jun 19 '15 at 17:57
  • If you don't want to get downvoted, and you want help, post enough information in your question so that iOS developers know enough about the problem to be able to help you. – Duncan C Jun 19 '15 at 19:09

1 Answers1

4

Depending on where/how you get the Java Date, your best bet would be to get the milliseconds and instantiate the NSDate with it.

So call the getTime() method on the Java Date to get milliseconds since Epoch, then create your NSDate with the dateWithTimeIntervalSince1970: method. The NSDate method takes seconds (as a floating point), so divide by 1000.0 to keep the precision (thanks to Martin R for pointing this out in comments). :)

Java Util Date method

NSDate method


There seems to be some confusion on what exactly is being asked. To be as general as possible, time objects typically have a method to get the milliseconds since epoch and a constructor (or setter) to pass in the seconds since epoch. So all you have to do is get the seconds from one object and instantiate the other object with the seconds.

BlakeP
  • 429
  • 4
  • 8
  • 2
    NSDate takes seconds as a *floating point* value and has sub-second precision, so you should divide by 1000.0 instead of "chopping off" – Martin R Jun 19 '15 at 16:23
  • My lack of objective-c knowledge is showing! Method said seconds, didn't dig into the actual types. Updated answer. ;) – BlakeP Jun 19 '15 at 16:26
  • This answer has nothing to do with the question!!! – Michael Jun 19 '15 at 16:37
  • @confile: your questions is absolutely vague and leaves us guessing. – vikingosegundo Jun 19 '15 at 16:39
  • @confile: Is JavaUtilDate not the j2objc mapping of java.util.Date? Does it not have a getTime() method? – Martin R Jun 19 '15 at 16:47
  • I don't understand why this answer is down voted. This is exactly what I would have said, based on the given information. – Martin R Jun 19 '15 at 16:53
  • From what I have read of j2objc, it sounds like it translates all the methods of the object you convert. So there should be an equivalent of getTime. I appreciate the support Martin. :) – BlakeP Jun 19 '15 at 17:00