-2

I want to make trial version for some java applications depend on date..does the date class retrieved from user's pc so he can trick ? Or it's calculated ?

Ahmad.ak
  • 93
  • 6
  • What do you mean by "so he can trick"? – Basil Bourque Nov 11 '16 at 06:56
  • change device's date – Ahmad.ak Nov 12 '16 at 23:30
  • **If you cannot trust the user, then you cannot trust their computer‘s clock**, and you cannot trust the current time as reported by the operating system or the JVM. Use a time server that you trust, one on the local network or one over the internet. Or use alternative hardware to obtain current time. Discussed already on many other questions and answers on Stack Overflow. – Basil Bourque Nov 13 '16 at 00:42

2 Answers2

2

The date information will indeed come from the system date on the computer it's running on. So if this is a web application with the java code running on the server, that's where it will come from. If it's a standalone java program that you're sharing with others, it will get the date from the user's computer.

There's not really anywhere else it could come from if you think about it: computers don't have a secret, "correct" date and a separate user-modifiable one.

nvioli
  • 4,137
  • 3
  • 22
  • 38
  • Hypothetically, you could fetch the time with ntp or sntp (or the header field from a web response) - but you are correct in that that isn't how Java does it. – Elliott Frisch Nov 10 '16 at 21:22
  • @ElliottFrisch good point, that's actually one way for OP to accomplish what they're looking for: call a server for the time instead of using java's date. – nvioli Nov 10 '16 at 21:26
1

It is calculated from the user's system clock using epoch time (number of milliseconds since January 1, 1970 at midnight GMT).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • True in Java 8 and earlier. In Java 9 a new implementation of [`Clock`](https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html) means the current time such as `Instant.now()` is captured in up to **nanoseconds** resolution rather than milliseconds. Actual accuracy depends on your computer clock hardware. – Basil Bourque Nov 11 '16 at 06:54