Skip to lesson

Instant, time zones and IST

An Instant is one exact moment on the world's timeline, the same everywhere. A ZonedDateTime shows that moment as a date and clock time in one place, such as 8:00 PM in India, which is 10:30 AM in New York on the same day.

Instant: a point on the UTC timeline

An Instant is an exact point on the timeline in UTC, used for timestamps; it becomes a local date and time only when a time zone is applied.

UTC (Coordinated Universal Time) is the world's reference clock. It never changes for summer or winter. Every country's clock is UTC plus or minus some hours.

Example 1 · Instant: timestamps on the UTC timeline

InstantDemo.javaJava
import java.time.Duration;import java.time.Instant;
public class InstantDemo {    public static void main(String[] args) {        Instant epoch = Instant.ofEpochSecond(0);        Instant payment = Instant.parse("2024-03-15T14:30:00Z");   // in a real app: Instant.now()        Instant refund = payment.plus(Duration.ofHours(26));
        System.out.println("Epoch:        " + epoch);        System.out.println("Payment:      " + payment);        System.out.println("Epoch second: " + payment.getEpochSecond());        System.out.println("Epoch milli:  " + payment.toEpochMilli());        System.out.println("Refund:       " + refund);        System.out.println("Refund after payment? " + refund.isAfter(payment));        System.out.println("Gap:          " + Duration.between(payment, refund));    }}
Output
Epoch:        1970-01-01T00:00:00Z
Payment:      2024-03-15T14:30:00Z
Epoch second: 1710513000
Epoch milli:  1710513000000
Refund:       2024-03-16T16:30:00Z
Refund after payment? true
Gap:          PT26H

What this code does

  • An Instant is a count of seconds from 1970-01-01T00:00Z, and it always prints in UTC with a Z. Many APIs send the milliseconds form from toEpochMilli().
  • Moments are compared with isAfter and measured with Duration, the same way as before. No zone is involved yet.

An Instant stores a moment as seconds (plus nanoseconds) counted from the epoch, 1970-01-01 at 00:00 UTC. It prints in UTC with a Z at the end, such as 2024-03-15T14:30:00Z; Z means "zero offset", which is UTC. Logs, payment records and APIs use Instant because a moment does not depend on where the reader is. A UPI payment at 2024-03-15T14:30:00Z happened at one moment for both the sender in Chennai and the bank server in Singapore. Instant.now() reads the current moment; tests use Instant.parse(...) or Instant.ofEpochSecond(...) instead.

An Instant has no year, month or day of its own. To read those, you must first choose a zone.

ZoneId Asia/Kolkata and Indian Standard Time

A time zone is a region that shares one set of clock rules, such as "India" or "New York". Java names zones with region IDs like Asia/Kolkata, America/New_York and Europe/London, taken from a worldwide database of zone rules.

Example 2 · Indian Standard Time with Asia/Kolkata

IstDemo.javaJava
import java.time.DateTimeException;import java.time.Instant;import java.time.ZoneId;import java.time.ZoneOffset;import java.time.ZonedDateTime;
public class IstDemo {    public static void main(String[] args) {        ZoneId india = ZoneId.of("Asia/Kolkata");        Instant payment = Instant.parse("2024-03-15T14:30:00Z");
        ZonedDateTime inIndia = payment.atZone(india);        System.out.println("In India:   " + inIndia);        System.out.println("Offset:     " + inIndia.getOffset());        System.out.println("Local part: " + inIndia.toLocalDateTime());        System.out.println("Fixed offset: " + ZoneOffset.ofHoursMinutes(5, 30));
        try {            ZoneId.of("IST");        } catch (DateTimeException e) {            System.out.println("ZoneId.of(\"IST\") failed: " + e.getMessage());        }    }}
Output
In India:   2024-03-15T20:00+05:30[Asia/Kolkata]
Offset:     +05:30
Local part: 2024-03-15T20:00
Fixed offset: +05:30
ZoneId.of("IST") failed: Unknown time-zone ID: IST

What this code does

  • instant.atZone(india) shows the same moment on Indian clocks. 14:30 UTC plus 5 hours 30 minutes is 20:00, and the value prints its offset and zone.
  • The short name "IST" is not a valid zone ID, so use "Asia/Kolkata". A ZoneOffset of +05:30 is just a number, with none of the zone's rules.

Indian Standard Time is ZoneId.of("Asia/Kolkata"). Its offset, the fixed difference from UTC, is +05:30 all year, because India does not use daylight saving time. So 14:30 UTC is 20:00 in India. Avoid the short name "IST". ZoneId.of("IST") throws an exception, and in the wider world IST can also mean Irish or Israel Standard Time. A plain ZoneOffset.ofHoursMinutes(5, 30) is a fixed offset with no rules; prefer the region ID, which also knows each zone's history.

ZonedDateTime: converting a meeting time

A ZonedDateTime is a LocalDateTime plus a ZoneId, so it marks a real moment. Build it with ZonedDateTime.of(localDateTime, zone) or localDateTime.atZone(zone).

One Instant, three wall clocks. The moment 2024-10-15T04:30:00Z is 10:00 in Asia/Kolkata (UTC+5:30), 05:30 in Europe/London (UTC+1 in summer) and 00:30 in America/New_York (UTC-4 in summer), all on 15 October.

Example 3 · A meeting time for clients abroad

MeetingDemo.javaJava
import java.time.LocalDateTime;import java.time.ZoneId;import java.time.ZonedDateTime;
public class MeetingDemo {    public static void main(String[] args) {        ZonedDateTime call = LocalDateTime.of(2024, 3, 15, 20, 0).atZone(ZoneId.of("Asia/Kolkata"));        String[] zones = {"UTC", "Europe/London", "America/New_York", "Asia/Dubai", "Asia/Singapore"};
        System.out.println("Call in India: " + call.toLocalDateTime());        for (String z : zones) {            ZonedDateTime there = call.withZoneSameInstant(ZoneId.of(z));            System.out.println(z + ": " + there.toLocalDateTime() + " (" + there.getOffset() + ")");        }
        ZonedDateTime wrong = call.withZoneSameLocal(ZoneId.of("America/New_York"));        System.out.println("withZoneSameLocal: " + wrong.toLocalDateTime() + ", same moment? " + wrong.isEqual(call));    }}
Output
Call in India: 2024-03-15T20:00
UTC: 2024-03-15T14:30 (Z)
Europe/London: 2024-03-15T14:30 (Z)
America/New_York: 2024-03-15T10:30 (-04:00)
Asia/Dubai: 2024-03-15T18:30 (+04:00)
Asia/Singapore: 2024-03-15T22:30 (+08:00)
withZoneSameLocal: 2024-03-15T20:00, same moment? false

What this code does

  • withZoneSameInstant keeps the moment and changes the clock: 20:00 in India is 10:30 in New York and 14:30 in London. The offset shows how far each zone is from UTC on that date; a zero offset prints as Z.
  • withZoneSameLocal keeps 20:00 but moves it to New York, which is a different moment, so isEqual is false. That is almost never what a meeting invite needs.

To show the same moment for someone abroad, call withZoneSameInstant(otherZone). A call at 20:00 IST on 15 March 2024 is 10:30 in New York and 14:30 in London. toInstant() turns it back into an Instant, and instant.atZone(zone) goes the other way. The similar-looking withZoneSameLocal keeps the clock time and changes the moment: 20:00 in India becomes 20:00 in New York, a different moment entirely. Use it only when you really mean "the same clock time over there".

Daylight saving time in other countries

Daylight saving time (DST) means moving clocks one hour forward in spring and back in autumn, to get more evening daylight. India does not do this, but the USA, the UK and Europe do.

Example 4 · Daylight saving changes the gap

DstGapDemo.javaJava
import java.time.LocalDateTime;import java.time.ZoneId;import java.time.ZonedDateTime;
public class DstGapDemo {    public static void main(String[] args) {        ZoneId india = ZoneId.of("Asia/Kolkata");        ZoneId newYork = ZoneId.of("America/New_York");        ZoneId london = ZoneId.of("Europe/London");        String[] days = {"2024-01-15", "2024-03-15", "2024-04-15"};
        for (String d : days) {            ZonedDateTime call = LocalDateTime.parse(d + "T20:00").atZone(india);            ZonedDateTime ny = call.withZoneSameInstant(newYork);            ZonedDateTime ld = call.withZoneSameInstant(london);            System.out.println(d + " 20:00 IST -> New York " + ny.toLocalTime() + " (" + ny.getOffset()                    + "), London " + ld.toLocalTime() + " (" + ld.getOffset() + ")");        }    }}
Output
2024-01-15 20:00 IST -> New York 09:30 (-05:00), London 14:30 (Z)
2024-03-15 20:00 IST -> New York 10:30 (-04:00), London 14:30 (Z)
2024-04-15 20:00 IST -> New York 10:30 (-04:00), London 15:30 (+01:00)

What this code does

  • India stays at +05:30 all year, but New York moves from -05:00 to -04:00 on 10 March, and London from Z (+00:00) to +01:00 on 31 March. In mid-March only New York has changed.
  • The same 20:00 IST call is 09:30 in New York in January but 10:30 from March, and 14:30 in London until it becomes 15:30 in April. Zone rules handle this; a hard-coded "10 hours 30 minutes" would be wrong for most of the year.

Example 5 · A DST gap, and one day versus 24 hours

DstJumpDemo.javaJava
import java.time.Duration;import java.time.ZoneId;import java.time.ZonedDateTime;
public class DstJumpDemo {    public static void main(String[] args) {        ZoneId newYork = ZoneId.of("America/New_York");
        ZonedDateTime missing = ZonedDateTime.of(2024, 3, 10, 2, 30, 0, 0, newYork);        System.out.println("Asked for 02:30, got: " + missing);
        ZonedDateTime noon = ZonedDateTime.of(2024, 3, 9, 12, 0, 0, 0, newYork);        ZonedDateTime plusDay = noon.plusDays(1);        ZonedDateTime plus24h = noon.plusHours(24);        System.out.println("plusDays(1):   " + plusDay);        System.out.println("plusHours(24): " + plus24h);        System.out.println("Real length of that day: " + Duration.between(noon, plusDay));    }}
Output
Asked for 02:30, got: 2024-03-10T03:30-04:00[America/New_York]
plusDays(1):   2024-03-10T12:00-04:00[America/New_York]
plusHours(24): 2024-03-10T13:00-04:00[America/New_York]
Real length of that day: PT23H

What this code does

  • On 10 March 2024, New York clocks jumped from 02:00 straight to 03:00, so 02:30 did not exist. Java moves it forward by the length of the gap, to 03:30 at the new offset -04:00.
  • plusDays(1) keeps the clock time (noon to noon), which that day was only 23 hours. plusHours(24) adds exactly 24 hours and lands at 13:00.

In 2024, New York moved forward on 10 March and back on 3 November; London moved forward on 31 March and back on 27 October.

So the gap between India and other countries changes during the year. India to New York is 10 hours 30 minutes in winter but 9 hours 30 minutes in summer. India to London is 5 hours 30 minutes in winter and 4 hours 30 minutes in summer. Never hard-code these gaps; let ZoneId rules work them out for each date. DST also creates odd local times. On 10 March 2024, New York clocks jumped from 02:00 to 03:00, so 02:30 never happened; Java moves such a gap time forward by one hour, to 03:30. In November, 01:30 happens twice (an overlap); Java picks the earlier one by default. Adding one day across a DST change keeps the clock time, while adding 24 hours keeps the exact length.

Real-world rule: store in UTC, show in local time

Servers, databases and logs store moments as UTC, usually as Instant. Then every record can be compared and sorted directly, with no zone mix-ups and no DST confusion. A server in Mumbai and one in Frankfurt write the same value for the same moment.

Example 6 · Store in UTC, show in local time

StoreUtcDemo.javaJava
import java.time.Instant;import java.time.ZoneId;import java.util.ArrayList;import java.util.List;
public class StoreUtcDemo {    public static void main(String[] args) {        // what the database stores: one Instant per order        List<Instant> orders = new ArrayList<>();        orders.add(Instant.parse("2024-03-15T18:45:00Z"));        orders.add(Instant.parse("2024-03-15T04:10:00Z"));        orders.add(Instant.parse("2024-03-14T23:20:00Z"));        orders.sort(null);
        ZoneId india = ZoneId.of("Asia/Kolkata");        ZoneId newYork = ZoneId.of("America/New_York");        for (Instant o : orders) {            System.out.println(o + " | India " + o.atZone(india).toLocalDateTime()                    + " | New York " + o.atZone(newYork).toLocalDateTime());        }    }}
Output
2024-03-14T23:20:00Z | India 2024-03-15T04:50 | New York 2024-03-14T19:20
2024-03-15T04:10:00Z | India 2024-03-15T09:40 | New York 2024-03-15T00:10
2024-03-15T18:45:00Z | India 2024-03-16T00:15 | New York 2024-03-15T14:45

What this code does

  • The stored values are Instants, so sorting them gives the true order of events, whatever zone each customer was in.
  • Each user sees the same moment on their own clock. The order at 23:20 UTC on 14 March is already 15 March in India, but still 14 March in New York.

Convert to a local zone only at the edge, when you show the time to a person: instant.atZone(ZoneId.of("Asia/Kolkata")) for a user in India, America/New_York for one in New York. Store the user's zone ID as part of their profile. A LocalDateTime like 2024-03-15T20:00 is not enough for an event that people join from many places. Nobody can tell which 20:00 it means.

Next step

Check your understanding

11 quick questions