Share this page 

Set the computer clockTag(s): Date and Time JNI


Define the following prototype in the header file
JNIEXPORT void JNICALL Java_JavaHowTo_setSystemTime
  (JNIEnv *, jobject, jshort, jshort);
the JNI function
JNIEXPORT void JNICALL Java_JavaHowTo_setSystemTime
  (JNIEnv *env, jobject obj, jshort hour, jshort minutes) {
    
    SYSTEMTIME st;

    GetLocalTime(&st);  
    st.wHour = hour;      
    st.wMinute = minutes;  
    SetLocalTime(&st);   
}
The Java JNI wrapper would be
class JavaHowTo {
  public native void setSystemTime( short hour, short minutes);
  static {
    System.loadLibrary("javahowto"); 
  }
}
And finally, to use it
public class JNIJavaHowTo {
  public static void main(String[] args) {
    short hour = 10;
    short minutes = 21;
    
    // this example will set the system at 10h21 using the Windows API
    // SetLocalTime. 
  
    JavaHowTo jht = new JavaHowTo();
    // set the time at 10h21
    jht.setSystemTime(hour, minutes); 
    }
}