-1

I am developing client / server application using java. client and server could be in different time zones. I want to read time value from client and it must be validated in server side. so , I want to get Universal time, which should be same in all over the world. what function I can use in java?"

Nt: I already tried System.currentTimeMillis(). but it gives different time value on server and client

  • If `System.currentTimeMillis()` gives different values between the client and the server, it's because one (or both) of them is misconfigured and/or not synchronized. Do you use NTP ? – Florent Bayle Aug 28 '14 at 14:24
  • Nope,My server will act as cloud server. I'm not familiar with NTP. can I use NTP to the public network? – user3331329 Aug 28 '14 at 14:40
  • possible duplicate of [Daylight saving time and time zone best practices](http://stackoverflow.com/questions/2532729/daylight-saving-time-and-time-zone-best-practices) – Basil Bourque Aug 28 '14 at 15:18

2 Answers2

0

If you are using something before Java 8, you can use the Calendar class, specifically the overloaded getInstance method that takes a TimeZone as a parameter or the getInstance method that takes a TimeZone and a locale as a parameter. You can use any time zone, but it seems like Greenwich Mean Time or Universal Coordinated Time would be most appropriate for your needs.

Java 8 provides a new date/time package that you could use. Unfortunately, I'm not as familiar with this interface, but I would look at the Clock class and the Instant class.

Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
  • thanks Thomas, I'm using jdk 1.5. if I use TimeZone based time values then I should do conversion right. Is there any Online provider provider international time values? – user3331329 Aug 28 '14 at 14:43
  • @user3331329 I don't know what you mean by "conversion". Both sides could expect time in the same time zone. Java provides methods to obtain the names of the available time zones and information about them. Since you're using JDK 1.5, I'd dig into the documentation for `Calendar` and `TimeZone` and the other associated classes. – Thomas Owens Aug 28 '14 at 14:44
  • conversion means , if client is at a zone, and server is at another time zone, then we need to convert to server time zone right? – user3331329 Aug 28 '14 at 15:04
  • @user3331329 Not if everyone is using the same time zone to begin with. I'd recommend reading the documentation of the classes I mentioned and learning about how time zones are represented. – Thomas Owens Aug 28 '14 at 16:06
0

Short direct answer, using Joda-Time 2.4 library:

DateTime nowUtc = DateTime.now( DateTimeZone.UTC );

Do some searching rather than posting. This topic has been covered in hundreds if not thousands of answers.

In brief:

  • Learn UTC
  • Learn ISO 8601
  • Server computers should all be set to UTC time zone (or Reykjavík). Use OS setting, not Java.
  • Your programming should never assume the server is set to UTC; specify the time zone for adjustment as seen in code example above.
  • Server computers should all be connected to time servers, and routinely verified.
  • Never set default time zone in Java as that affects all threads of all apps running in that JVM.
  • Never depend on the current date-time obtained from a client machine. Their clocks cannot be assumed to be correct.
  • You can try to retrieve the user’s locale and time zone from client side. But if important, your app should ask the user.
  • Nearly all of your business logic and data storage (database) should be in UTC. Adjust to local time only for presentation where expected by the user.
  • Avoid java.time.Date and .Calendar classes bundled with Java. They are notoriously troublesome. They have been supplanted by the new java.time package bundled in Java 8. That package was inspired by Joda-Time but is re-architected. Joda-Time is remains viable, retains some advantages over java.time.
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154