my dog learned polymorphism*
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
Author

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.
Regards,
Naresh
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="http://www.auptyma.com" 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
www.DeveloperAdvantage.com - FREE Audiobooks for Software Developers
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I set System time?
 
Similar Threads
Date.compareTo : misbehaviour on linux environment?
Setting system date
Time related issues
Files & file systems
How to set system properties permanently?