Skip to content

Lesson 6 of 9 · 22m

Modern IO, HttpClient, Time, and the Death of Runnable Spaghetti

Replace legacy concurrency and utility habits with java.time, NIO, HttpClient, immutable collections, and straightforward service code.

A surprising amount of 'legacy Java' is not old syntax. It is old dependencies: Date, Calendar, blocking HttpURLConnection, hand-rolled retry loops, mutable singleton holders, and executor plumbing nobody can explain. Modernizing those pieces often produces more reliability than a fashionable architecture rewrite.

Dates are not strings, and time is not integers

Use java.time. Instant is a point in time, ZonedDateTime is that point in a place with rules, LocalDate is a calendar date without a time zone, and OffsetDateTime is often what databases and APIs exchange.

Do not subtract 86400 seconds to mean tomorrow. Daylight-saving transitions make that wrong. Parse strict input at the boundary and format only at the presentation boundary.

Use HttpClient like an adult

java.net.http.HttpClient is good enough for many service-to-service calls and removes the worst parts of HttpURLConnection:

HttpClient client = HttpClient.newBuilder()
    .connectTimeout(Duration.ofSeconds(2))
    .build();

HttpRequest request = HttpRequest.newBuilder(URI.create("https://billing.internal/v1/invoices/" + id))
    .timeout(Duration.ofSeconds(5))
    .header("Accept", "application/json")
    .GET()
    .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

Set both connect and request timeouts. Retries belong in a policy with jitter and idempotency awareness, not in a for loop someone copied from a forum post.

Blocking IO can now be boring

With virtual threads, simple blocking IO often scales adequately without a reactive pipeline.

Thread.startVirtualThread is fine for fire-and-forget examples and small servers, but prefer executor or scoped lifetimes in production so failure and shutdown are deliberate. Boring code is not negligent code.

Files and collections have better defaults now

Use Path and Files, not string concatenation and manual stream closing. Immutable collections remove whole categories of accidental mutation:

Map<String, String> headers = Map.of(
    "Accept", "application/json",
    "X-Trace-Id", traceId
);

For data that must be mutable, keep mutation local and expose unmodifiable views. Do not make everything immutable as a religion; make ownership obvious.

A small modernization sweep has high return

Search the codebase for new Date(, Calendar.getInstance, HttpURLConnection, new Thread(, Vector, Hashtable, SimpleDateFormat, and manually synchronized shared maps. Each hit is a conversation. ConcurrentHashMap, java.time, virtual threads, and immutable copies are often direct replacements with fewer footguns.

Field notes

  • HttpClient asynchronous methods still need explicit timeout and executor discipline; async is not automatically safer.
  • SimpleDateFormat is not thread-safe; use DateTimeFormatter or create a new formatter.
  • Virtual threads do not fix missing timeouts; every network call still needs a deadline.
  • Use Files.readString for small trusted files, not multi-gigabyte payloads.
  • Do not replace every dependency at once; batch upgrades by behavioral area and test the boundary.

Was this lesson helpful?

Sign in to rate