Production-grade timezone propagation for Spring Boot. Resolves the client's timezone once per request and makes it available everywhere β controllers, services, @Async threads, CompletableFuture chains, and JSON responses.
- Pluggable resolution β header, JWT claim, or fixed timezone
- Async propagation β
TaskDecoratorfor@Async,SamayCompletableFuture, andSamayTaskExecutor - Convenience API β
Samay.now(),Samay.atZone(),Samay.format(),Samay.startOfDay() - Jackson integration β opt-in per-field timezone conversion with
@SamayFormat - JPA support β
SamayZoneAttributeConverterfor UTC storage
| Artifact | Purpose |
|---|---|
samay-core |
Interceptor, strategies, async support, convenience API, JPA converter |
samay-jackson |
Jackson auto-configuration with @SamayFormat annotation |
samay-spring-boot-starter |
Auto-configuration β add this to your project |
<dependency>
<groupId>io.github.dineshsolanki</groupId>
<artifactId>samay-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
<!-- Optional: per-field JSON timezone conversion -->
<dependency>
<groupId>io.github.dineshsolanki</groupId>
<artifactId>samay-jackson</artifactId>
<version>4.0.0</version>
</dependency># βββ Core ββββββββββββββββββββββββββββββββββββββββββββ
samay.enabled=true # disable to turn off all timezone handling
samay.strategy=header # header | jwt | fixed
samay.header-name=X-TimeZone # HTTP header name (header strategy)
samay.jwt-claim=timezone # JWT claim name (jwt strategy)
samay.fixed-zone=UTC # timezone ID (fixed strategy)
samay.thread.inheritable=false # use InheritableThreadLocal for child threads
# βββ Jackson (optional) βββββββββββββββββββββββββββββ
samay.jackson.auto-convert=false # set true to enable @SamayFormat conversion
samay.jackson.global=false # set true to convert ALL date fields (not just annotated)
samay.jackson.date-time-pattern=yyyy-MM-dd'T'HH:mm:ssXXX@RestController
public class EventController {
@GetMapping("/events")
public List<EventDto> getEvents() {
ZoneId userZone = Samay.getZoneId();
// use userZone for queries, formatting, etc.
}
}ZonedDateTime userNow = Samay.now();
ZonedDateTime display = Samay.atZone(entity.getCreatedAt());
String formatted = Samay.format(entity.getCreatedAt(), "MMM dd, yyyy HH:mm");
// Date-range queries against UTC-stored timestamps
Instant start = Samay.startOfDay(selectedDate);
Instant end = Samay.endOfDay(selectedDate);// Option A: SamayTaskExecutor (pre-configured)
@Bean
public SamayTaskExecutor taskExecutor() {
SamayTaskExecutor executor = new SamayTaskExecutor();
executor.setCorePoolSize(4);
return executor;
}
// Option B: TaskDecorator with your own executor
@Bean
public TaskDecorator samayTaskDecorator() {
return new SamayTaskDecorator();
}
// Option C: CompletableFuture with timezone propagation
SamayCompletableFuture.supplyAsync(() -> {
// Samay.getZoneId() works here β timezone propagated from caller
return service.findEvents(Samay.now());
});Annotate only the fields that should be timezone-converted:
public class EventDto {
@SamayFormat
private Instant startTime; // converted to user's timezone in JSON
private Instant createdAt; // stays UTC
}
// application.properties
// samay.jackson.auto-convert=trueSet samay.jackson.global=true to convert all date/time fields without annotating each one.
// Entity β store as UTC
@Column(name = "created_at")
@Convert(converter = SamayZoneAttributeConverter.class)
private Instant createdAt;
// Display in user's timezone
ZonedDateTime display = Samay.atZone(entity.getCreatedAt());
// Date-range queries
Instant start = Samay.startOfDay(selectedDate);
Instant end = Samay.endOfDay(selectedDate);samay.strategy=jwt
samay.jwt-claim=timezoneExtracts timezone from the JWT payload (base64-decoded, no signature verification). Falls back to the X-TimeZone header if no JWT is present. Requires jackson-databind on the classpath.
samay.strategy=fixed
samay.fixed-zone=Asia/Kolkata| v3.x | v4.0 |
|---|---|
Samay.getTimeZone() β TimeZone |
Samay.getZoneId() β ZoneId |
| Single jar | Multi-module: add samay-spring-boot-starter |
| Header only | Header, JWT, or fixed strategy |
| No async support | SamayTaskDecorator, SamayCompletableFuture |
| No JSON support | @SamayFormat per-field conversion |
Samay.getTimeZone() is deprecated but still works.
GPL-3.0 β see LICENSE.