Spring boot 2 Converting Duration java 8 application.properties
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot 2 can bind application.properties values directly into Java 8 Duration fields, which makes timeout-style configuration much cleaner than manual parsing. The main ideas are simple: use a Duration property in your configuration class, write a supported duration string in properties, and optionally define a default unit when the property has no suffix.
Bind directly to java.time.Duration
A common setup uses @ConfigurationProperties:
Then in application.properties:
Spring Boot converts the string into a Duration automatically.
Use supported units intentionally
Spring Boot supports suffixes such as:
- '
nsfor nanoseconds' - '
msfor milliseconds' - '
sfor seconds' - '
mfor minutes' - '
hfor hours' - '
dfor days'
Examples:
This is usually easier to read than raw numeric millisecond values, especially when configuration files grow.
ISO-8601 format also works
You may also see ISO-8601 duration syntax such as:
That is valid, but for ordinary application configuration many teams prefer the shorter Boot-style suffix form because it is easier to scan.
Use @DurationUnit when no suffix is present
If you want a bare number in properties and a default unit in code, annotate the field:
Now this property means seconds by default:
Without a suffix or @DurationUnit, the default interpretation may not match what you expect, so it is better to be explicit.
Use the bound duration in application code
Once bound, the value behaves like any ordinary Java Duration:
That means you can keep your configuration readable while still getting strongly typed time values in the codebase.
Prefer explicit units in shared configurations
In teams, configuration files often outlive the code review context that created them. Explicit suffixes such as 30s or 500ms are usually easier for the next engineer to understand than a bare 30 paired with an annotation hidden in Java code. @DurationUnit is useful, but explicit unit strings in properties are often even clearer operationally.
Common Pitfalls
The most common mistake is forgetting the unit suffix and assuming Spring Boot will guess the intended unit the way you meant it.
Another common issue is mixing styles without documenting them, for example some properties using raw numbers and others using explicit suffixes.
People also bind the property as a String and parse it manually even though Spring Boot can already bind it directly to Duration.
Finally, if you want unitless values in properties, add @DurationUnit so the default is part of the code contract instead of tribal knowledge.
Summary
- Spring Boot 2 can bind property values directly to Java 8
Durationfields. - Use clear suffixes such as
ms,s,m, andhinapplication.properties. - ISO-8601 durations also work, but short suffixes are often easier to read.
- Use
@DurationUnitwhen properties omit the unit suffix. - Prefer direct
Durationbinding over manual string parsing.
Related reading
- Spring Boot 2 health actuator default mapping
- Spring Boot 3.0.0, SQS java.lang.ClassNotFoundException org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver
- Spring boot 3 - Jakarta and Javax
- Spring Boot 3 springdoc-openapi-ui doesn't work
- Spring Boot 3.x upgrade. Could not resolve org.springframework.bootspring-boot-gradle-plugin3.0.1
- Spring boot 404 error custom error response ReST
- Spring Boot + Kafka + Kerberos configuration
- Spring Boot / Kafka Json Deserialization - Trusted Packages

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.