Samay (Formerly TimeZoneInterceptor) Help

Using Samay

This section explains how to use the main features of the Samay library in your Spring Boot application.

Installing Samay

<dependency> <groupId>io.github.dineshsolanki</groupId> <artifactId>Samay</artifactId> <version>RELEASE</version> </dependency>
implementation 'io.github.dineshsolanki:Samay:RELEASE'

Timezone Extraction

By default, Samay expects the timezone in a request header named X-TimeZone.

To extract and store timezone on each request:

@RestController public class ExampleController { @GetMapping("/endpoint") public String handler(HttpServletRequest request) { // Samay extracts timezone from request // and stores in thread local } }

No need to manually extract the timezone. Samay handles it automatically.

Accessing Timezone

To access the extracted timezone in your controllers or services:

import io.github.dineshsolanki.Samay; @Service public class MyService { public void businessLogic() { TimeZone tz = Samay.getTimeZone(); // use timezone as needed } }

The timezone is available via Samay.getTimeZone() anywhere downstream.

Changing Header Name

To customize the request header name:

samay.header-name=My-Timezone-Header

Samay will now check My-Timezone-Header instead of default X-TimeZone.

Enabling thread inheritance

The default behavior of Samay is to not inherit the timezone information in child threads. You can enable thread inheritance by adding the following property to your application.properties:

samay.thread.inheritable=true

Disable Auto Configuration

To disable the auto configuration in Spring Boot:

@SpringBootApplication(exclude = {SamayAutoConfiguration.class}) public class MyApp { }

And manually configure the interceptor:

@Bean public TimeZoneInterceptor timeZoneInterceptor() { return new TimeZoneInterceptor(); }

ThreadLocal Cleanup

Samay automatically cleans up the thread local after the request is handled. No need for manual removal.

Last modified: 21 October 2023