0

Possible Duplicate:
How can I set the System Time in Java?

I want to set the system time from a Java program. I've been googling and the only solution I found involves Runtime.exec. This has the really fat disadvantage that Windows also has no reasonable system command to set the system time (e.g. how do I get the date locale?).

I am now using winapi through jna. This is fun (no irony).

Now I wondered if no one ever bothered to make an OS independent Java library to set the system time. The question seems to come up often enough.

Community
  • 1
  • 1
peter
  • 429
  • 2
  • 7
  • 16
  • 1
    It's probably because no one except OS-provided tools usually need to set the system time. – Thilo Oct 10 '12 at 07:49
  • 1
    This is a command that changes the OS behavior, and you don't want any Java program to escalate until the admin privileges so easily. Due to this security reason, I guess you won't have another option. – Luiggi Mendoza Oct 10 '12 at 07:49
  • You could mock the system time: http://virgo47.wordpress.com/2012/06/22/changing-system-time-in-java/ – Werner Kvalem Vesterås Oct 10 '12 at 07:50
  • @LuiggiMendoza: The same can be said for deleting files. – Thilo Oct 10 '12 at 07:50
  • 1
    @Thilo: I disagree. Deleting files can be a pretty common operation for a Java application. Just think about deleting temporary files. Also: OS already have a good mechanism for allowing processes to delete *some* files while keeping others safe. Those mechanisms don't usually exist for the system time (which is a global settings in all OS I know of). – Joachim Sauer Oct 10 '12 at 07:52
  • @Aviram thanks, as i said, i googled and also already read that thread. if there is a solution in there, please point me to it. – peter Oct 10 '12 at 07:52
  • I think the OS has good mechanisms to prevent non-admin processes from changing the system time, too. Or at least it should. If you don't like the file example, how about binding to sub-1024 ports? I agree that setting the system time is not a common operation for *any* application, Java or not. The assertion I challenged was that this (access to admin functions) has anything to do with Java. – Thilo Oct 10 '12 at 07:56
  • 1
    *"The question seems to come up often enough."* - Don't confuse this with some indication of real demand. Basically, you would be hard pressed to find a genuine need to be able to set the clock portably from Java. (And some guy at home thinks it would be cool does not count as genuine need in my books.) – Stephen C Oct 10 '12 at 07:57
  • I suspect there is no support in Java because the designers couldn't imagine why this would be a good idea when there are many secure, specialist services for correcting the system time automatically. e.g. NTP – Peter Lawrey Oct 10 '12 at 08:04
  • yeah, for some reason ntp doesnt work from my network location, so i am writing a program that gets the time from an http header and uses that as system time. maybe anyone knows a (lightweight, cli) program for that? – peter Oct 10 '12 at 08:08
  • how do you guys see this as a duplicate? are you reasoning that from the lack of the mentioning of such a library in the other thread i have to infer that there is none? i felt the need to clarify that since it seemed quite unbelievable to me. – peter Oct 10 '12 at 08:11
  • 2
    @peter: "exact duplicate" means that you're asking for the same thing. it does *not* necessarily mean that the other question has an answer that you'd find helpful (but that would be a good bonus, of course). – Joachim Sauer Oct 10 '12 at 08:23
  • If you need NTP but it doesn't work, you or your admins should fix it. Replacing it with your own system is unlikely to be a good idea. – Peter Lawrey Oct 10 '12 at 08:36
  • i have no control over the network and am in no position to demand anything from the admins. - but that is besides the point here. – peter Oct 10 '12 at 10:16
  • @peter - then use the tools that you *are* provided with (e.g. the control panel) to change the clock by hand. If you can't do that, then a hypothetical Java solution wouldn't work either. – Stephen C Oct 10 '12 at 22:59
  • @peter - The same question asked again because the answers are not satisfactory is a "duplicate question" ... because the question has already been asked. If you want better answers, then post a bonus. You have enough reputation points to do that. – Stephen C Oct 10 '12 at 23:02
  • Not all problems at work are technical and don't always demand a technical solution. You should get support from your management that you either need to more accurate times (in which case your work should provide you with what you need) or you don't need it in which case you shouldn't do it. I would be concerned whenever you find yourself doing something you don't believe your work would support and suffering in silence is rarely a good idea. – Peter Lawrey Oct 11 '12 at 05:27

0 Answers0