Explain Codes LogoExplain Codes Logo

Convert LocalDateTime to LocalDateTime in UTC

java
datetime
utility-class
date-formatting
Nikita BarsukovbyNikita Barsukov·Sep 5, 2024
TLDR
LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime utcDateTime = localDateTime .atZone(ZoneId.systemDefault()) // Attach good old systemDefault zone. .withZoneSameInstant(ZoneOffset.UTC) // Let's take a trip to UTC. Pack your offsets! .toLocalDateTime(); // Voilà, UTC equivalent of your LocalDateTime.

Convert your LocalDateTime to its UTC equivalent by wrapping it in the systemDefault zone, heed the call of the UTC siren with withZoneSameInstant(ZoneOffset.UTC) and voilà! you get it in local date-time format with toLocalDateTime().

Daylight saving blues

Transitioning through daylight saving time (DST) can be as confusing as assembling flat pack furniture without instructions. Thankfully, ZonedDateTime has a built-in instruction manual for handling DST transitions. So when your LocalDateTime decides to time-travel during a DST transition, withZoneSameInstant is your flux capacitor ensuring accurate conversion to UTC.

Take time with UTC

If your work demands that your clock always reads UTC, you could buy a second watch or better, play it smarter with Clock object:

Clock utcClock = Clock.systemUTC(); // UTC's hotline! LocalDateTime utcNow = LocalDateTime.now(utcClock); // UTC time, anytime. Told ya, we're smarter.

Clock.systemUTC() is your hotline to UTC time, irrespective of your system’s default time zone.

Utility class: Conversion made easy

If you are repeatedly converting times to UTC much like a time traveller, utility class is your TARDIS:

public class DateTimeUtils { public static LocalDateTime toUtc(LocalDateTime localDateTime) { return localDateTime .atZone(ZoneId.systemDefault()) // Attach systemDefault. Hold tight, no seat belts! .withZoneSameInstant(ZoneOffset.UTC) // Time to hit UTC. No jet lag, promise. .toLocalDateTime(); // Congrats, you're in UTC now. } // Add your space-time coordinates (or methods) here… }

Creating a utility class is like making your favorite dish. Gather all the ingredients (methods) in one place for consistent, repeatable yummy outcomes.

Precise conversions: Details matter

Any time traveller worth their salt knows the devil is in the details. So, when converting times to UTC, zone rules and validation are as important as your towel on Hitchhiker's travels.

Ensure your conversion code remains infallible by testing edge cases, like unexpected side trips to DST cutover periods or unannounced changes in local time zone definitions.

@Test public void testConversionToUTC() { LocalDateTime beforeDST = LocalDateTime.of(2023, 3, 26, 1, 30); // The day DST decided to play a prank LocalDateTime afterDST = DateTimeUtils.toUtc(beforeDST); assertEquals(LocalDateTime.of(2023, 3, 26, 0, 30), afterDST); // Correct time after DST sobers up }

Dumbledore's Pensieve: specific requirements

Sometimes you just want the offset without the full UTC conversion. It's like sneaking a peek into Dumbledore's Pensieve, getting the memory (or offset) you need:

ZoneOffset currentOffset = ZonedDateTime.now(ZoneId.systemDefault()).getOffset();

Or perhaps you need to turn a LocalDateTime into an Instant. It's like adding another time-turner to your collection:

Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();

This can be handy when an Instant is what stands between you and that API you’re trying to woo.