Java LocalDateTime atOffset() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, dt.atOffset(offset) combines a LocalDateTime with a fixed ZoneOffset, such as +05:30 or Z for UTC. The result is an OffsetDateTime, which is a real moment on the timeline.

The local date and time stay exactly the same. Only the offset is added, so the moment is now known.

An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file.

Syntax

Pass a ZoneOffset. Use ZoneOffset.UTC for UTC, or ZoneOffset.ofHoursMinutes() for others.

Java
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

OffsetDateTime india = dt.atOffset(ZoneOffset.ofHoursMinutes(5, 30));  // 2026-03-15T10:30+05:30
OffsetDateTime utc = dt.atOffset(ZoneOffset.UTC);                      // 2026-03-15T10:30Z
CallWhat it does
dt.atOffset(offset)The date-time with a fixed offset.
odt.toInstant()The exact moment, as an Instant.
odt.toEpochSecond()The seconds since 1 January 1970 UTC.
odt.toLocalDateTime()Drops the offset, and gives back the original LocalDateTime.

An offset is fixed. It knows nothing about daylight saving time. For that, use atZone() with a region such as Asia/Kolkata.

Library declarations
Java
// java.time.LocalDateTime
public OffsetDateTime atOffset(ZoneOffset offset)

Parameters

ParameterDescription
offset (ZoneOffset)The offset from UTC, such as ZoneOffset.UTC. It must not be null.

Return Value

Return ValueDescription
OffsetDateTimeThe same local date and time, combined with the offset.

Compare with Similar Methods

Ways to give a date-time a zone or offset:

MethodHow it differs
atZone(zone)Uses a named region, which knows daylight saving time rules.
LocalDateTime.ofInstant(instant, zone)Goes the other way: from a moment to a local date-time.
LocalDateTime.ofEpochSecond(...)Builds a date-time from epoch seconds and an offset.

Java Examples

Example 1 — Attach an Offset to a Local Date-Time

Add the offset of India and the UTC offset to the same date-time.

Java
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class LdtAtOffsetDemo {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

        OffsetDateTime india = dt.atOffset(ZoneOffset.ofHoursMinutes(5, 30));
        OffsetDateTime utc = dt.atOffset(ZoneOffset.UTC);

        System.out.println("india: " + india);
        System.out.println("utc:   " + utc);
        System.out.println("local part is the same: " + india.toLocalDateTime().equals(dt));
    }
}

How It Works

  1. One local date-time. It is 10:30 on 15 March 2026.
  2. India. The offset +05:30 is added to the end of the text.
  3. UTC. UTC is written as the letter Z.
  4. The local part is untouched. toLocalDateTime() gives the original value back.

Example 2 — Get the Epoch Seconds

Turn a local date-time into epoch seconds by choosing an offset. The offset changes the answer.

Java
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class LdtAtOffsetEpoch {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

        long utcSeconds = dt.atOffset(ZoneOffset.UTC).toEpochSecond();
        long indiaSeconds = dt.atOffset(ZoneOffset.ofHoursMinutes(5, 30)).toEpochSecond();

        System.out.println("UTC:   " + utcSeconds);
        System.out.println("India: " + indiaSeconds);
        System.out.println("gap in seconds: " + (utcSeconds - indiaSeconds));
    }
}

How It Works

  1. The same local time. Two offsets give two different moments.
  2. UTC. 10:30 UTC on 15 March 2026 is 1,773,570,600 seconds after 1 January 1970.
  3. India. 10:30 in India happened 5 hours 30 minutes earlier, so the number is smaller.
  4. The gap. 19,800 seconds is exactly 5 hours 30 minutes.

Example 3 — See Which Moment It Really Is

Convert both values to an Instant to see the real moment in UTC, and compare them.

Java
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class LdtAtOffsetInstant {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 10, 30);

        OffsetDateTime india = dt.atOffset(ZoneOffset.ofHoursMinutes(5, 30));
        OffsetDateTime utc = dt.atOffset(ZoneOffset.UTC);

        System.out.println("india instant: " + india.toInstant());
        System.out.println("utc instant:   " + utc.toInstant());
        System.out.println("same moment:   " + india.isEqual(utc));
        System.out.println("same local:    " + india.toLocalDateTime().isEqual(utc.toLocalDateTime()));
    }
}

How It Works

  1. An Instant is always in UTC. It shows the real moment, with the letter Z at the end.
  2. India is earlier. 10:30 at +05:30 is 05:00 in UTC.
  3. Different moments. isEqual on the OffsetDateTime values prints false.
  4. Equal local parts. The date-time without the offset is the same, so the last line prints true.

Frequently Asked Questions

If the local time is already in UTC, use dt.atOffset(ZoneOffset.UTC). If it is in another zone, use dt.atZone(zone).withZoneSameInstant(ZoneOffset.UTC).
atOffset() uses a fixed offset such as +05:30. atZone() uses a region such as Asia/Kolkata, which knows daylight saving time changes.
Use dt.toEpochSecond(ZoneOffset.UTC), or dt.atOffset(offset).toEpochSecond().
No. The local date and time stay the same. Only the offset is added.
Use ZoneOffset.UTC, ZoneOffset.ofHours(2), ZoneOffset.ofHoursMinutes(5, 30), or ZoneOffset.of("+05:30").

Next: atZone()

Attach a time zone to a date-time and get a ZonedDateTime.

atZone() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful