0

I'm using Java Instant for my use-case where I want to capture the exact moment in timeline for.eg, { When the user last visited my website }. This is when the data gets persisted in the backend (sql). It persists in UTC format as I use Instant.now();

As my website is used across all Zones (In the world), When the user lands on the homepage, I want to show the dateTime (when he last visited the website) as localDateTime as per his zoneId.

How do I get the ZoneId Information?

As my website is currently hosted in 2 servers and both servers are in India, If I do ZoneId.systemDefault(), I always get Indian ZoneId (Asia/Kolkota). So, the conversion is not possible.

If Someone from US hit's my server, How do I get the zoneId as US in my server?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 2
    You can provide a `ZoneId` manually, but then you need to determine if it corresponds with the IP address accessing your service. – deHaar Jul 26 '19 at 06:09
  • 1
    You could use the IP address to guess the zone, or use a piece of Javascript on the front end to either convert your UTC timestamp to the browser's local zone, or send that zone to your server to use for the session. – vlumi Jul 26 '19 at 06:14

1 Answers1

4

The best would be to keep things simple.

Just use UTC to store visit time. Frontend can convert UTC into browser's (thus user's) timezone.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99