2

I have created an application in Java Spring MVC, Hibernate and AngualarJS, In the application I am disabling certain date and time. The application is working fine for localhost. but when I hosted the application the date and time disabling seems wrong. My server is located in UK and when I tried to access from India its disabling wrong time and date. In my java code which resides in server I have used the following code for getting the current date time

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));

Can anyone please tell me some solution for this.

How can we get the client user date time zone in server side

Alex Man
  • 4,746
  • 17
  • 93
  • 178

2 Answers2

3

Get the time on client's browser and send it to your web server.

Alternatively, you can use the client's IP to determine the timezone and then their local time.

  • how we can get the client IP and get the date time in server side – Alex Man Jul 03 '15 at 11:10
  • I would recommend the IP because sometimes users don't have their computer time updated. But in some cases in which the user is using a proxy or whatever, the time then maybe incorrect. Anyway to get the IP use getInetAddress which returns the address to which the socket is connected. To get the timezone, you will first need to get the city and then using that piece of data, you will need to figure out the timezone. If you want to keep it simple however, just get it on client's computer and send it back to your web server. –  Jul 03 '15 at 11:18
  • I couldn't figure out how you could do it by IP so just get it on the client's computer and send it to your web server. Code: http://pastebin.com/uNPy1hKh Also I am sorry for my late reply! –  Jul 03 '15 at 11:41
  • If user is using VPN or other means of tunnelling, he will get a completely different IP address... – We are Borg Jul 03 '15 at 15:13
  • We are Borg: That's what i said. Read through all of the comments carefully before making one. –  Jul 04 '15 at 03:02
0

get the time from the server , the server is always updated . when you run the app on the server you ill get the server time , when you run on the client you ill also get the client time

Date d1 = new Date(); SimpleDateFoenter code herermat df = new SimpleDateFormat("MM/dd/YYYY HH:mm a"); String formattedDate = df.format(d1);

you can visit How to get current server time in Java?

Community
  • 1
  • 1
Zak
  • 26
  • 3