How to show timezones in GMT format in java
Posted By : Shiv Kumar | 29-Sep-2015
We all required date and time along with timezones in our project at various stages like sign up, event management, time management and etc. Java provides class java.util.TimeZone to play with time zones.It provides various methods to do so.
In my project, the requirement was to show the time zones in GMT format along with city name.
TimeZone class provides method getAvailableIDs() which gives a string array in the following format:
Pacific/Midway
Pacific/Niue
America/Adak
Etc/GMT+10 and so on...
But what I required was all the times zones in GMT format along with city name only like :
(GMT-11:00) Midway
(GMT-11:00) Niue
(GMT-10:00) Adak
(GMT-10:00) Atka
After some work around with the available list provided by getAvailableIDs(), finally I got the list in desired manner with the below code snippet :
I created a custom taglib to show all the time zones in gsp :
def timeZones = { attrs,body ->
def allTimeZones = TimeZone.getAvailableIDs();
List timeZoneList = []
allTimeZones?.each{
def timeZone = TimeZone.getTimeZone(it) // fetch timezone object from string name
long hours = TimeUnit.MILLISECONDS.toHours(timeZone?.getRawOffset()); // find hours from offset value to display
// find minutes from offset value to display
long minutes = TimeUnit.MILLISECONDS.toMinutes(timeZone?.getRawOffset()) - TimeUnit.HOURS.toMinutes(hours);
minutes = Math.abs(minutes);
String result = "";
if (hours > 0) {
result = String.format("(GMT+%d:%02d) %s", hours, minutes, timeZone?.getID());
} else if (hours < 0){
result = String.format("(GMT%d:%02d) %s", hours, minutes, timeZone?.getID());
}else{
result = "(GMT)"+' '+ timeZone?.getID();
}
timeZoneList.add(result)
}
out << body(timeZoneList:timeZoneList)
}
def timeZoneDisplayName = { attrs->
def zoneName = attrs?.timeZoneName.trim()
def zoneParts = zoneName.split(' ')
def locationName = ''
if(zoneParts[1]?.indexOf('/') == -1)
locationName = zoneParts[1]
else
locationName = zoneParts[1].substring(zoneParts[1].lastIndexOf("/") + 1)
def displayName = zoneParts[0]+' '+locationName.replace('_', ' ')
out << displayName
}
In gsp use taglib like :
<select multiple="multiple" name="timeZone" id="addTimeZone">
<event:timeZones>
<g:each in="${timeZoneList}">
<option value="${it.toString().trim()}"><event:timeZoneDisplayName timeZoneName="${it}"/></option>
</g:each>
</event:timeZones>
</select>
This workaround provides me the desired data.
Applying small changes you can also get the time zones in your required format if format is different from this one.
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shiv Kumar
Shiv is an experienced Java Developer with a strong background in multiple technologies. He specializes in defining system architectures to ensure reliable and resilient solutions. He possesses a comprehensive understanding of the latest technologies and has hands-on experience in Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as relational databases like MySQL and PostgreSQL, and non-relational databases like MongoDB. He excels in API implementations, Microservices, Web Services development, testing, and deployments. Shiv actively contributes to code enhancements and consistently delivers valuable contributions to various client projects, including Fabtrack, Pando, Pandojo, Digikam, WhatsApp Integration, Croniz, Punchin Application, Script TV, Bhaasha, and more. He demonstrates strong analytical skills and a creative mindset. In addition, he has a passion for reading books and exploring new technologies and innovations.