উদাহরণ সহ Instant Class ব্যবহার

Instant Class - জাভা টাইম প্যাকেজ (Java.time Package) - Java Technologies

299

Instant ক্লাসটি Java 8 তে java.time প্যাকেজে অন্তর্ভুক্ত হয়েছে এবং এটি UTC (Coordinated Universal Time) টাইম স্ট্যাম্পের একটি নির্দিষ্ট মুহূর্ত (epoch time) প্রতিনিধিত্ব করে। এটি অত্যন্ত সঠিক সময়ের মান প্রদান করে, যা সময়সীমার নির্দিষ্ট মুহূর্ত (milliseconds বা nanoseconds) হিসেবে স্টোর হয়। Instant সাধারণত time-based calculations এবং timestamps তৈরি করতে ব্যবহৃত হয়, যেখানে সময় অঞ্চল (time zone) বা সময়ের পার্থক্য কোনও ভূমিকা রাখে না।

Instant ক্লাসের প্রধান বৈশিষ্ট্য:

  1. Instant.now(): বর্তমান সময়ের Instant পাবেন (এটি UTC সময় অঞ্চল অনুসারে হবে)।
  2. Instant.ofEpochSecond(long epochSecond): epoch সেকেন্ড (1970-01-01T00:00:00Z থেকে শুরু) থেকে Instant তৈরি করবেন।
  3. Instant.ofEpochMilli(long epochMilli): epoch মিলিসেকেন্ড থেকে Instant তৈরি করবেন।
  4. plusSeconds(long seconds) এবং minusSeconds(long seconds): Instant এ সেকেন্ড যোগ বা বিয়োগ করা।

Instant ক্লাসের উদাহরণ সহ ব্যবহার:

1. Instant.now() - বর্তমান সময় পাওয়া:

import java.time.Instant;

public class InstantExample {
    public static void main(String[] args) {
        // Get the current Instant (UTC time)
        Instant currentInstant = Instant.now();
        System.out.println("Current Instant: " + currentInstant);
    }
}

আউটপুট:

Current Instant: 2024-12-23T09:34:12.123456Z

ব্যাখ্যা:

  • Instant.now() মেথডটি বর্তমান UTC সময় (Coordinated Universal Time) দেয় এবং এটি সেকেন্ড এবং ন্যানোসেকেন্ড পর্যন্ত নির্ভুল হয়।

2. Instant.ofEpochSecond() - Epoch সেকেন্ড থেকে Instant তৈরি:

import java.time.Instant;

public class InstantEpochSecondExample {
    public static void main(String[] args) {
        // Create an Instant from epoch second (1970-01-01T00:00:00Z)
        Instant instantFromEpoch = Instant.ofEpochSecond(1609459200); // 2021-01-01T00:00:00Z
        System.out.println("Instant from epoch second: " + instantFromEpoch);
    }
}

আউটপুট:

Instant from epoch second: 2021-01-01T00:00:00Z

ব্যাখ্যা:

  • Instant.ofEpochSecond(1609459200) 1970-01-01T00:00:00Z থেকে 1609459200 সেকেন্ড পরের সময়কে একটি Instant এ পরিণত করে, যা 2021-01-01T00:00:00Z এর সমান।

3. Instant.ofEpochMilli() - Epoch মিলিসেকেন্ড থেকে Instant তৈরি:

import java.time.Instant;

public class InstantEpochMilliExample {
    public static void main(String[] args) {
        // Create an Instant from epoch milliseconds
        Instant instantFromEpochMilli = Instant.ofEpochMilli(1617235200000L); // 2021-04-01T00:00:00Z
        System.out.println("Instant from epoch milli: " + instantFromEpochMilli);
    }
}

আউটপুট:

Instant from epoch milli: 2021-04-01T00:00:00Z

ব্যাখ্যা:

  • Instant.ofEpochMilli(1617235200000L) 1970-01-01T00:00:00Z থেকে 1617235200000 মিলিসেকেন্ড পরের সময়কে Instant এ পরিণত করেছে, যা 2021-04-01T00:00:00Z এর সমান।

4. plusSeconds() এবং minusSeconds() - সেকেন্ড যোগ বা বিয়োগ করা:

import java.time.Instant;

public class InstantManipulationExample {
    public static void main(String[] args) {
        // Create an Instant for the current time
        Instant currentInstant = Instant.now();
        System.out.println("Current Instant: " + currentInstant);

        // Add 1000 seconds to the current time
        Instant instantPlus1000Sec = currentInstant.plusSeconds(1000);
        System.out.println("Instant after adding 1000 seconds: " + instantPlus1000Sec);

        // Subtract 1000 seconds from the current time
        Instant instantMinus1000Sec = currentInstant.minusSeconds(1000);
        System.out.println("Instant after subtracting 1000 seconds: " + instantMinus1000Sec);
    }
}

আউটপুট:

Current Instant: 2024-12-23T09:40:00.123456Z
Instant after adding 1000 seconds: 2024-12-23T09:56:40.123456Z
Instant after subtracting 1000 seconds: 2024-12-23T09:23:20.123456Z

ব্যাখ্যা:

  • plusSeconds(1000) মেথডটি বর্তমান Instant এর সাথে 1000 সেকেন্ড যোগ করে।
  • minusSeconds(1000) মেথডটি বর্তমান Instant থেকে 1000 সেকেন্ড বিয়োগ করে।

5. Duration দিয়ে দুটি Instant এর মধ্যে পার্থক্য বের করা:

import java.time.*;

public class InstantDurationExample {
    public static void main(String[] args) {
        // Create two Instant objects
        Instant start = Instant.now();
        Instant end = start.plusSeconds(5000); // Adding 5000 seconds to start

        // Calculate the duration between the two Instants
        Duration duration = Duration.between(start, end);
        System.out.println("Duration in seconds: " + duration.getSeconds());
    }
}

আউটপুট:

Duration in seconds: 5000

ব্যাখ্যা:

  • Duration.between(start, end) মেথডটি দুটি Instant এর মধ্যে সময়ের পার্থক্য বের করে, যা এখানে 5000 সেকেন্ড।
  • duration.getSeconds() এই পার্থক্যকে সেকেন্ডে রিটার্ন করে।

ZonedDateTime এবং Instant Comparison:

Instant এবং ZonedDateTime এর মধ্যে পার্থক্য হলো, Instant UTC (Universal Time) এর নির্দিষ্ট মুহূর্তকে নির্দেশ করে, যেখানে ZonedDateTime নির্দিষ্ট সময় অঞ্চল সহ তারিখ এবং সময় প্রতিনিধিত্ব করে।

যখন আপনি Instant ব্যবহার করেন, তখন আপনি সময় অঞ্চল থেকে মুক্ত থাকেন, কিন্তু ZonedDateTime ব্যবহার করলে আপনি সময় অঞ্চলকে মান্য করে সময়ের হিসাব করতে পারেন।


  • Instant ক্লাসটি UTC সময়ের নির্দিষ্ট মুহূর্ত (epoch time) ধারণ করে এবং এটি সেকেন্ড ও ন্যানোসেকেন্ড পর্যন্ত নির্ভুল।
  • Instant ব্যবহার করে আপনি সময়ের পার্থক্য বের করতে পারেন, সময়ের যোগ-বিয়োগ করতে পারেন এবং সময়সীমার একটি নির্দিষ্ট মুহূর্ত তৈরি করতে পারেন।
  • ZonedDateTime এর সঙ্গে তুলনা করলে, Instant কোনও নির্দিষ্ট সময় অঞ্চলের সাথে সম্পর্কিত নয়, তবে এটি সার্বজনীন (UTC) সময়ের একটি নির্দিষ্ট মুহূর্ত নির্দেশ করে।
Content added By
Promotion

Are you sure to start over?

Loading...