How to Get IANA/Olsen Time Zones for a Country using Java
Time zones can be difficult to keep track of. However, getting time zone information in your app shouldn’t be a challenge — it should be a swift, automatic response whenever the need arises. With our IANA/Olsen Time Zone API, you can retrieve a country’s time zone instantly using only the country name (or three-letter country code). In response, this API will provide all available time zone data about that input country (one or multiple time zones depending on the country), including a base UTC offset value and current time stamp for each returned time zone. Before returning this information, it will validate the success of the operation, returning the full country name and all available country codes. For your reference, I’ve included an example response model below for the input “United States.”
The following input:
{
"CountryCodeOrName": "United States"
}
Will return the following response (JSON example):
{
"Successful": true,
"CountryFullName": "United States",
"ISOTwoLetterCode": "US",
"FIPSTwoLetterCode": "US",
"ThreeLetterCode": "USA",
"Timezones": [
{
"Name": "Eastern Standard Time",
"BaseUTCOffset": "-05:00:00",
"Now": "2022-10-28T15:49:14.822372"
},
{
"Name": "Central Standard Time",
"BaseUTCOffset": "-06:00:00",
"Now": "2022-10-28T14:49:14.822372"
},
{
"Name": "Mountain Standard Time",
"BaseUTCOffset": "-07:00:00",
"Now": "2022-10-28T13:49:14.822372"
},
{
"Name": "Alaskan Standard Time",
"BaseUTCOffset": "-09:00:00",
"Now": "2022-10-28T11:49:14.822372"
},
{
"Name": "Pacific Standard Time",
"BaseUTCOffset": "-08:00:00",
"Now": "2022-10-28T12:49:14.822372"
},
{
"Name": "Hawaiian Standard Time",
"BaseUTCOffset": "-10:00:00",
"Now": "2022-10-28T09:49:14.822372"
}
]
}
In the remainder of this article, I will demonstrate how you can utilize this API by structuring your API call with the ready-to-run Java code examples provided below. In addition to copying from the below code, however, you’ll also need a Cloudmersive API key (you can get one for free by registering a free account on our website) to authenticate the service.
Beginning installation with Maven, let’s first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, let’s add a reference to the pom.xml dependency, allowing JitPack to automatically compile the library:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Next, let’s shift focus to our controller and add the imports to the top of our file:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.AddressApi;
Lastly, let’s call the function, passing our parse request & API key through their respective fields (indicated by the code comments):
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");AddressApi apiInstance = new AddressApi();
GetTimezonesRequest input = new GetTimezonesRequest(); // GetTimezonesRequest | Input request
try {
GetTimezonesResponse result = apiInstance.addressGetTimezone(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressGetTimezone");
e.printStackTrace();
}
After that, we’re all done — no more code required.