A practical guide to migrating legacy Java code to java.time
Retiring java.util.Date and Calendar does not require rewriting an entire application. A staged migration can place Instant, LocalDate, and OffsetDateTime beside existing code, then move each boundary deliberately while production traffic continues to expose gaps. Learn more about Implementing A File Processing.
The key is to preserve behaviour before improving it. Inventory every date field, parser, formatter, database column, message payload, and external API, especially where midnight, offsets, or daylight-saving transitions are treated as business values.
Build a behavioural baseline
Legacy date code often looks simple until defaults enter the picture. SimpleDateFormat depends on locale and time zone, Calendar is mutable, and java.sql.Timestamp mixes SQL storage concerns with an instant. Record how current code handles nulls, invalid dates, shortened months, and values arriving near midnight.
Use representative samples from real systems rather than invented edge cases alone. A legacy file processing example can help identify where parsed dates enter batch jobs, while audit logs and payment records reveal the values customers and operators actually rely on.
Avoid changing time zones during the first pass. Capture the legacy default zone, host configuration, locale, and database session zone so later differences are explainable. Container deployments can silently alter defaults, making an explicit ZoneId safer than relying on the server setting.
Map legacy types to java.time
Choose the type from the business meaning, not the old class name. Use Instant for a point on the global timeline, LocalDate for a wall-clock date without a zone, and LocalDateTime for a local date and time. Keep a numeric offset with OffsetDateTime; use ZonedDateTime when a region and its daylight-saving rules matter.
java.sql.Date normally maps to LocalDate, while SQL timestamps often map to Instant when the contract denotes an absolute event. java.sql.Time has no direct java.time equivalent and is frequently better replaced by LocalTime or Duration. Keep ambiguous legacy fields at adapter boundaries until their meaning is established.
Convert at the edge before changing internal signatures. A small adapter prevents a database type from leaking through every layer:
Instant occurredAt = timestamp.toInstant();
LocalDate invoiceDate = sqlDate.toLocalDate();
Returning typed values from this boundary makes accidental comparisons and mixed-zone arithmetic easier to detect.
Convert parsing and formatting safely
SimpleDateFormat is not thread-safe, accepts lenient values, and formats according to mutable defaults. Replace shared instances with a reusable DateTimeFormatter, build it once with an explicit locale, and select strict resolution where invalid input should fail.
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.forLanguageTag("en-AU"))
.withResolverStyle(ResolverStyle.STRICT);
LocalDate issueDate = LocalDate.parse(text, formatter);
Preserve an existing wire format until its consumers are understood. Do not assume ISO-8601 is universally safe: LocalDateTime.parse("2025-04-06T02:30:00") has no offset, and ISO instants require Z or an explicit offset. Retain source text when exact rendering or audit evidence matters, then parse it into a clear type.
Handle Australian zones and schedules
Australian deployments make zone handling a business concern. Sydney and Melbourne use AEST (UTC+10) and shift to AEDT (UTC+11) during daylight-saving periods; Brisbane remains on AEST, and Perth uses AWST (UTC+8). During the daylight-saving overlap, one local time can represent two instants, so schedules should use ZonedDateTime or convert from Instant in the intended zone.
Public holidays vary by state and affect payroll, bank cut-offs, and retailer campaigns. Australian banks, payment providers, and retailers expect dependable local-time cut-offs and notifications, so a service used in Adelaide or Canberra must not assume a date valid in Sydney is valid everywhere.
Operational records also require care. Tax records in Australia generally need to be retained for five years, while the Privacy Act 1988 and Australian Privacy Principles require transparent handling of personal information where they apply. Keep source timestamps and conversion metadata for invoices, customer support records, and access logs when auditability matters.
Test conversions around real boundaries
Unit tests should lock down type selection, parsing, and conversion before integration tests cover persistence and APIs. Use Clock for deterministic values, and cover leap days, year boundaries, nulls, and the daylight-saving gap and overlap. An Instant rendered in Sydney during AEDT must remain the same instant when viewed in Brisbane.
Automation should exercise both the application and its dependencies. The Azure Functions testing guide is relevant where legacy processing is being lifted into serverless jobs: run the same fixture locally and in Azure, then compare database values, emitted messages, and logs.
Treat changed offsets or formatted strings as contract changes. Search snapshots, CSV fixtures, support scripts, and partner documentation for dd/MM/yy assumptions. If external consumers depend on ambiguous output, introduce versioned formatting rather than silently changing it during the library upgrade.
Roll out with adapters and safeguards
Deploy conversion behind adapters at database, queue, and HTTP boundaries. A legacy service can still accept java.util.Date while translating to Instant internally, and responses can temporarily reproduce the old format. Track remaining legacy imports, parser failures, unexpected offsets, and reconciliation differences to show whether ambiguity is falling.
Roll out by cohort and keep a reversible switch while comparing results with production traffic. Clock supports controlled testing of expiry windows, while Duration and Period make calculations explicit: Duration measures elapsed time on the timeline, whereas Period measures date-based amounts such as months.
Once consumers are stable, remove adapters rather than preserving two date models indefinitely. Explicit zones, immutable values, and clearer contracts then give teams a safer base for services operating across Australian states, trading partners, and daylight-saving schedules.