Search code examples
javaandroidc++cjava-native-interface

JNI return Date


I have JNI method that operates with const struct tm myTimeInfo. At the end, I would like to return result to Java. However, I cannot directly return Date. So far I convert struct tm to jstring and in java back to Date, which seems odd. Is there a way how to return directly Date filled with struct tm?

My current solution is something like:

JNIEXPORT jstring JNICALL package_getTimeLineEndUTC(JNIEnv *env, jobject thiz) {
    const struct tm timeInfo = generateTime();
    return env->NewStringUTF(asctime(&timeInfo));
}

Solution

  • Instead of returning a string, you can return a long, i.e milliseconds since the epoch:

    const struct tm timeInfo = generateTime();
    return mktime(&timeInfo) * 1000;
    

    Then use Date(long date) on the java side.