0

I have developed an desktop application using Java Swing and there are some features required and depends on the date.

Now the problem is if I do this: System.out.println(new Date());

will print this Tue Nov 03 16:29:11 EET 2015.

Now if I change the date in Windows calendar this date will also change, and I don't need the new Date() to change with OS Date changes? how can I do it?

prasun
  • 7,073
  • 9
  • 41
  • 59
swing
  • 1
  • 2
  • http://stackoverflow.com/questions/2510649/how-to-make-my-java-app-get-global-time-from-some-online-clock – Shivam Nov 03 '15 at 14:32
  • please i my app will deploy on on a local machine and doesn't have internet connection – swing Nov 03 '15 at 14:35
  • If you don't trust the system time after a change, why do you trust it before the change? – BadZen Nov 03 '15 at 14:37
  • There is only one date supported by OS, Java itself obviously can't provide you with date. You need to decide which source you want. – talex Nov 03 '15 at 14:39
  • without a n/w conection it would be hard to achieve – prasun Nov 03 '15 at 14:40
  • You need to create your own calender then. – Shivam Nov 03 '15 at 14:53
  • @Shivam nice idea but how can i make it in java and how can i connect to it if it would be running in the machine. – swing Nov 03 '15 at 15:12
  • @swing seriously you wanna create your own calender? I don't know how to, sorry!!!. Anyway if you able to create that the major problem arrive to set the current time every time after your application got stopped and you restart it else you need to run your application continuously. – Shivam Nov 03 '15 at 15:29
  • I'm voting to close this question as off-topic because it is asking for something that is logically impossible. – Harry Johnston Nov 03 '15 at 23:13
  • why should something logically impossible be voted as off-topic, who knows OS or computer vendor in future comes with a different sort of clock APIs in future, sounds weird but , who knows how technology innovates – prasun Nov 04 '15 at 19:14

1 Answers1

1

Hardware clock

There is only one source of time-keeping within your local computer: the hardware clock built into the box, managed by the host operating system (OS).

The JVM sources the current moment from the host OS, and therefore from the hardware clock. The JVM maintains its own current default time zone through which it interprets/adjusts that moment. But the moment, the point on the timeline, is reported from your hardware clock via the OS, with Java only on the listening end of that conversation.

So, yes, if a user with admin privileges alters the setting of the host computer hardware clock incorrectly, then yes, Java will report an incorrect date-time.

If you Question is, "how do I get the current moment in Java from my local computer without trusting my local computer?", well, that is logically impossible.

If determining the current moment accurately is critical to your app, and you do not trust the clock on the machine hosting your JVM, then you will need an outside source of time.

  • Usually that means making a query of a time server over the internet. But you say your JVM does not have access to the Internet.
  • One alternative is contacting a time server on your local network. Of course, that means trusting the sysadmin for that time server keeps it properly set. Note that conventional computer clocks are commonly unreliable, drifting a few seconds per month or worse. So a local time server must be kept synchronized with a reliable time source.
  • Another alternative is accessing another hardware clock external to the host machine. For example, a radio clock or GPS/Galileo/GLONASS satellite device as discussed here, here, and here.

photo of a couple of external radio clock devices


FYI, the terrible Date is now legacy, not to be used. Replaced by Instant. Both represent a moment in UTC (despite the lie told to you by Date::toString).

Instant instant = Instant.now() ;  // Capture the current moment in UTC as determined by your host OS & host hardware.

To see that moment through the wall-clock time used by the people of a particular region, apply a time zone (ZoneId) to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154