【问题标题】:JNI return DateJNI 返回日期
【发布时间】:2018-02-08 15:47:44
【问题描述】:

我有使用const struct tm myTimeInfo 操作的JNI 方法。最后,我想将结果返回给 Java。但是,我不能直接返回Date。到目前为止,我将struct tm 转换为jstring 并在java 中转换回Date,这似乎很奇怪。有没有办法直接返回Date填满struct tm

我目前的解决方案是这样的:

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

【问题讨论】:

    标签: java android c++ c java-native-interface


    【解决方案1】:

    您可以返回long,而不是返回字符串,即自纪元以来的毫秒数:

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

    然后在java端使用Date(long date)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多