In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to set JVM time zone under linux". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to set JVM time zone under linux".
How to set JVM time zone 1. Overview
When it comes to timestamps, users of our application can be very demanding. They want our application to automatically detect its time zone and display a timestamp in the correct time zone.
In this tutorial, we will introduce several ways to modify the JVM time zone. We will also learn about some pitfalls related to managing the time zone.
two。 Time zone introduction
By default, JVM reads time zone information from the operating system. This information is passed to the TimeZone class, which stores the time zone and calculates daylight saving time.
We can call the method getDefault, which returns the time zone where the program is running. In addition, we can use TimeZone. GetAvaableID () gets the list of supported time zone ID from the application.
When naming time zones, Java relies on the naming convention of the tz database.
3. Change the time zone
In this section, we will introduce several ways to change the JVM time zone.
3.1. Set environment variabl
Let's first look at how to use environment variables to change the time zone. We can add or modify the environment variable TZ.
For example, in a Linux-based environment, we can use the export command:
Export TZ= "America/Sao_Paulo"
After setting the environment variable, we can see that the time zone of the application we are running is now US / Sao_Paulo:
Calendar calendar = Calendar.getInstance (); assertEquals (calendar.getTimeZone (), TimeZone.getTimeZone ("America/Sao_Paulo")); 3.2. Set JVM parameters
The alternative to setting environment variables is to set the JVM parameter user.timezone. This JVM parameter takes precedence over the environment variable TZ.
For example, you can use the flag-D when running an application:
Java-Duser.timezone= "Asia/Kolkata" com.company.Main
Similarly, we can set the JVM parameter from the application:
System.setProperty ("user.timezone", "Asia/Kolkata")
We can now see that the time zone is Asia / Calcutta:
Calendar calendar = Calendar.getInstance (); assertEquals (calendar.getTimeZone (), TimeZone.getTimeZone ("Asia/Kolkata")); Set the time zone from the application
Finally, we can also use the TimeZone class to modify the JVM time zone from the application. This method takes precedence over environment variables and JVM parameters.
Setting the default time zone is simple:
TimeZone.setDefault (TimeZone.getTimeZone ("Portugal"))
As expected, the time zone is now Portugal:
Calendar calendar = Calendar.getInstance (); assertEquals (calendar.getTimeZone (), TimeZone.getTimeZone ("Portugal")); 4. Trap 4.1. Use three-letter time zone ID
Even if you can use a three-letter ID to represent the time zone, it is not recommended.
Instead, we should use longer names because the three-letter ID is ambiguous. For example, IST can be Indian Standard time, Irish Standard time, or Israeli Standard time.
4.2. Global Settin
Note that each of the above methods sets the time zone globally for the entire application. However, in modern applications, setting the time zone is often more subtle than this.
For example, we might need to convert the time to the end user's time zone, so the global time zone doesn't make sense. If you do not need a global time zone, consider specifying the time zone directly on each date-time instance. Partition date time or offset date time is a convenient class.
Thank you for reading, the above is the content of "how to set JVM time zone under linux". After the study of this article, I believe you have a deeper understanding of how to set JVM time zone under linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.