0

I'm using JSP for a website, and there I have to display time. I have previously tried using LocalDate.now() and Date together with Calendar, like this:

Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

But this also gave me UTC Time instead of PST. I can't set it manually with ZoneSet because all user's will have a different timezone. How can I fix this? I'm using Java 8

Murad
  • 167
  • 1
  • 2
  • 13
  • When you create a `Calendar` with no arguments, it's set to the current time. When you create a `Date` with no arguments, it's set to the current time. So immediately setting the time on the calendar with the date does nothing. Also, only use the `java.time` package, not the legacy classes in `java.util`. – erickson Jul 08 '22 at 17:28

1 Answers1

3

Java runs on the server, it cannot access the client's clock. If you want the time on the client's computer, you need to use JavaScript's Date object.

See How to get the exact local time of client?.

Another option is to allow users to specify their timezone, and use this to localize times when displayed to the user. You would typically provide a drop-down in some kind of Account Settings page.

But in either case, the time (or timezone) comes from the client. You can make guesses on the server using, for example, geolocation services, but you cannot actually know what the user's local time is from the server.

user229044
  • 232,980
  • 40
  • 330
  • 338