简体   繁体   中英

Java 8 : Using Instant, How to retrieve ZoneId information

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM