java
48 lines · 5 steps
A custom ISO-week Converter in Spring
A small immutable value type plus a registered Spring Converter that turns strings like 2024-W07 into a typed week.
Explained by
highlit
1package com.example.web.convert;
2
3import java.time.LocalDate;
4import java.time.format.DateTimeFormatter;
5import java.util.Locale;
6
7import org.springframework.core.convert.converter.Converter;
8import org.springframework.format.Formatter;
9import org.springframework.lang.NonNull;
10import org.springframework.stereotype.Component;
11
12public final class IsoWeek {
13
14 private final LocalDate monday;
15
16 private IsoWeek(LocalDate monday) {
17 this.monday = monday;
18 }
19
20 public LocalDate getMonday() {
21 return monday;
22 }
23
24 public LocalDate getSunday() {
25 return monday.plusDays(6);
26 }
27
28 static IsoWeek parse(String value) {
29 String[] parts = value.split("-W");
30 if (parts.length != 2) {
31 throw new IllegalArgumentException("Expected format yyyy-Www, got: " + value);
32 }
33 int year = Integer.parseInt(parts[0]);
34 int week = Integer.parseInt(parts[1]);
35 LocalDate monday = LocalDate.ofYearDay(year, 1)
36 .with(java.time.temporal.WeekFields.ISO.weekOfWeekBasedYear(), week)
37 .with(java.time.temporal.WeekFields.ISO.dayOfWeek(), 1);
38 return new IsoWeek(monday);
39 }
40
41 @Component
42 public static class StringToIsoWeekConverter implements Converter<String, IsoWeek> {
43 @Override
44 public IsoWeek convert(@NonNull String source) {
45 return IsoWeek.parse(source.trim());
46 }
47 }
48}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Modeling a domain concept as an immutable value type keeps parsing and derived data in one trustworthy place.
- 2Implementing Spring's Converter interface lets the framework map request strings to rich types automatically.
- 3Annotating a converter with @Component registers it in Spring's conversion service without manual wiring.
Related explainers
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
java
public class SlidingLogRateLimiter { private final int maxRequests; private final long windowMillis;
How a sliding-log rate limiter works
rate-limiting
concurrency
sliding-window
Advanced
8 steps
java
public final class Slugifier { private static final Pattern NON_LATIN = Pattern.compile("[^\\w-]"); private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
Building a URL slugifier in Java
regex
unicode-normalization
string-processing
Intermediate
8 steps
java
@Repository public interface OrderRepository extends JpaRepository<Order, Long> { @Query("""
Keyset pagination with Spring Data JPA
pagination
cursor
jpa
Advanced
8 steps
java
public record FixedWidthField(String name, int start, int end) { public String extract(String line) { int from = Math.min(start, line.length());
Parsing fixed-width text in Java
records
parsing
streams
Intermediate
7 steps
java
@RestController @RequestMapping("/api/quotes") public class QuoteStreamController {
Streaming market quotes with Spring WebFlux
reactive-streams
backpressure
server-sent-events
Advanced
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-custom-iso-week-converter-in-spring-explained-java-c104/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.