File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How do I set System time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do I set System time?" Watch "How do I set System time?" New topic

How do I set System time?

Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
Hi all,
I would like to know if there's any way to set the system time using java, just as we can get the system time using
Please let me know how I can set the system time or atleast whether it is possible or not.
Virag Saksena
Ranch Hand

Joined: Nov 27, 2005
Posts: 71
java gets the system time from the underlying operating system
On most systems, setting time requires superuser privileges,
so you cannot do it from vanilla Java.

To do this essentially will require you to
#1. Create a native library to do setuid to become superuser
#2. Call settimeofday (or equivalent system call)
#3. Create a java wrapper to expose this as a java call

Doing this creates inherent security risks and breaks normal Java conventions and is not recommended.

What are you trying to do which requires you to set the system time from Java ?

<a href="" target="_blank" rel="nofollow">The Peak of Performance</a>
Don Morgan
Ranch Hand

Joined: Jul 24, 2003
Posts: 84
An alternative to JNI would be to try use the java runtime to exec whatever command is appropriate to the system you are using.

Don Morgan, Founder - FREE Audiobooks for Software Developers
I agree. Here's the link:
subject: How do I set System time?
It's not a secret anymore!