The OffsetDateTime class represents a date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system (e.g., 2024-06-01T10:15:30+05:30)
Commonly Used Methods

Simple Program
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class OffsetDateTimeExample {
public static void main(String[] args) {
OffsetDateTime currentTime = OffsetDateTime.now();
OffsetDateTime customTime = OffsetDateTime.of(2024, 6, 1, 10, 30, 0, 0, ZoneOffset.of("+05:30"));
System.out.println("Current OffsetDateTime: " + currentTime);
System.out.println("Custom OffsetDateTime: " + customTime);
}
}Problem Statement
LotusJavaPrince and LotusPythonPrince are building a secure messaging platform where all timestamps must be recorded in UTC but displayed in the sender’s local offset.
They need to:
- Capture message send time in UTC (
OffsetDateTimewith+00:00offset). - Convert and display it in sender’s local offset (e.g.,
+05:30or-04:00). - Maintain consistency across global users.
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class SecureMessageTimestamp {
public static void main(String[] args) {
System.out.println("Secure Messaging System by LotusJavaPrince & Mahesh");
// Step 1: Capture current message time in UTC
OffsetDateTime utcTime = OffsetDateTime.now(ZoneOffset.UTC);
System.out.println("Message Timestamp (UTC): " + utcTime);
// Step 2: Simulate sender from India (+05:30)
ZoneOffset indiaOffset = ZoneOffset.of("+05:30");
OffsetDateTime indiaTime = utcTime.withOffsetSameInstant(indiaOffset);
System.out.println("Display in India (IST): " + indiaTime);
// Step 3: Simulate sender from New York (-04:00)
ZoneOffset nyOffset = ZoneOffset.of("-04:00");
OffsetDateTime nyTime = utcTime.withOffsetSameInstant(nyOffset);
System.out.println("Display in New York (EDT): " + nyTime);
}*/
}
/*
Secure Messaging System by LotusJavaPrince & Mahesh
Message Timestamp (UTC): 2025-05-22T08:48:55.983178Z
Display in India (IST): 2025-05-22T14:18:55.983178+05:30
Display in New York (EDT): 2025-05-22T04:48:55.983178-04:00
*/The OffsetDateTime class is ideal when:
- You want to store or transfer date-times with precise UTC offsets.
- You don’t need the full complexity of time zones (
ZoneId), just an offset. - You’re dealing with APIs, databases, or logs across different regions.
It offers a clean and deterministic way to represent date-time values that are offset from UTC, making it essential for auditing, scheduling, and international messaging systems.
