0

I'm trying to capture end user's time with their local timezone in Java (Example: 10:00AM PST), but my code is defaulting to UTC time and timezone, which is server's time where my code is running. Any help is greatly appreciated. Thank you!

import java.time.*;

ZoneId z = ZoneId.systemDefault();
ZonedDateTime zdt = ZonedDateTime.now(z);
String issueTime = zdt.format(DateTimeFormatter.ofPattern("hh:mma z"));
  • Do you have end user country details, you can give in ZoneId like ZoneId z = ZoneId.of("Australia/Darwin");, you can get end user country details using Locale. – Shubham Pathak Mar 03 '22 at 04:47
  • Are all your users in the same timezone? – tgdavies Mar 03 '22 at 06:24
  • "end user" in what context? You haven't told us anything about the application - is this a web app? A mobile app? Something else? The code you've shown will find the system time zone of whatever system it's running on - so yes, if that's a web server, it will be the server's time zone rather than the user's time zone. We can't really give any more advice without knowing more information – Jon Skeet Mar 03 '22 at 07:08
  • Hi, yes my app is a web application running on a web logic server that is set UTC by default. My end users are in different timezone across United States (CST, MDT, EST, PST). End users meaning the actual users accessing the application from their browser. So, my requirement is to capture the end users time with timezone in hh:mma z format. With my code posted above, it is capturing the servers time where my code is running instead of end users from where they are accessing my application. I hope I cleared your question. Any help would be appreciated. – javalearner Mar 03 '22 at 17:54
  • @JonSkeet could you please suggest any solution ? – javalearner Mar 04 '22 at 04:44
  • See https://stackoverflow.com/questions/3001260/how-to-detect-the-timezone-of-a-client – tgdavies Mar 04 '22 at 05:05
  • See the *many* duplicates. I'd also suggest you edit your question to provide all the necessary information instead of just putting it in comments - and understand that a single UTC offset (your "hh:mm:a z format") is *not* a time zone. – Jon Skeet Mar 04 '22 at 07:54

1 Answers1

0

tl;dr

You seem to want to produce a string in a certain format for the time-of-day with a zone-offset indicator.

ZonedDateTime
.now( 
    ZoneId.systemDefault() 
)
.format( 
    DateTimeFormatter.ofPattern("hh:mma z") 
)

Same code:

ZonedDateTime.now( ZoneId.systemDefault() ).format( DateTimeFormatter.ofPattern("hh:mma z") )

You could omit the explicit ZoneId part, though I recommend keeping it to make clear your intentions.

ZonedDateTime.now().format( DateTimeFormatter.ofPattern("hh:mma z") )

You said:

my code is defaulting to UTC time and timezone

That means the JVM’s current default time zone was set to UTC.

Verify your actual time zone

Your code works.

I suspect your JVM has a current default time zone of UTC. Servers should generally be set to UTC as their default time zone.

Add a call to z.toString() to verify what default zone is in play.

ZoneId z = ZoneId.systemDefault();
System.out.println( z.toString() ) ;  // 

Here is a slightly modified version of your code. See this code run live at IdeOne.com. Notice how we specify a time zone for the ZonedDateTime.

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;  // ZoneId.systemDefault();
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

DateTimeFormatter f = DateTimeFormatter.ofPattern("hh:mma z") ; 
String issueTime = zdt.format( f );

System.out.println( issueTime ) ;

10:25PM PST

By the way, I recommend automatically localizing rather than hard-coding a specific format. See DateTimeFormatter.ofLocalized… methods.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Hello, Thank you for your response. America/LosAngeles would retrieve only PST. But my end users are in different time zones across United States ( PST, MDT, EST, CST). I have to capture the particular users system time with their respective timezone dynamically. – javalearner Mar 03 '22 at 17:49
  • @javalearner As shown in my code comments, you can retrieve the user’s current default time zone by calling `ZoneId.systemDefault()`. Also, be aware that 2-4 letter abbreviations such as PST, MDT, EST, CST are *not* time zones. [Real time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) are named with `Continent/Region` format, such as `Europe/Paris`. – Basil Bourque Mar 04 '22 at 03:55
  • If you see in my code snippet, I already used ZoneId.systemDefault which is retrieving my servers default time which is set to UTC, not end users who is accessing the web app from browser. I understand the 2-4 letters are not abbreviations, I need the real timezone which is Continent/Region of the end user. For example, if the end user accessing the app resides in California, it should their respective timezone which is America/Los Angeles, if the user resides in Texas, it should retrieve America/Chicago. I cannot set ZoneId to specific timezone as my end users are distributed across US. – javalearner Mar 04 '22 at 04:43
  • @javalearner So I am not understanding your problem then. How is `ZonedDateTime.now( ZoneId.systemDefault() ).format( DateTimeFormatter.ofPattern("hh:mma z") )` not working for you? – Basil Bourque Mar 04 '22 at 04:50
  • It is working, but it is retrieving the UTC time which is my server default set time, not end user time. For example, If I'm a user, accessing a web app from Texas and the web app is deployed and running on a server which is set to UTC timezone. My requirement is to capture Texas time which is central(America/Chicago) timezone, not UTC. ZoneId.systemDefault is resulting in UTC, not America/Chicago time. I hope it is clear now. – javalearner Mar 04 '22 at 04:57
  • @javalearner No that was not clear at all in your Question. You need to rewrite your Question. The JVM running the backend of your web site runs on the server, so of course it sees the clock on the server. If you are asking how to capture the current moment as seen on a web browser, that is a *JavaScript* question, not a Java question. Specify your details, such as are you using Jakarta Servlet technology on the server side? – Basil Bourque Mar 04 '22 at 05:01