Java Technologies Date এবং Time API Example: Java 8 এর Date এবং Time API ব্যবহার গাইড ও নোট

477

Java 8 থেকে Date and Time API (যা java.time প্যাকেজে অন্তর্ভুক্ত) যোগ করা হয়েছে, যা পুরানো java.util.Date এবং java.util.Calendar এর চেয়ে অনেক সহজ, সঠিক এবং সুবিধাজনক। এটি LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Instant, Duration, এবং Period সহ আরও অনেক ক্লাস প্রদান করে। এই API টির মাধ্যমে আপনি তারিখ এবং সময়ের সাথে কাজ করতে পারবেন অনেক বেশি পরিষ্কারভাবে এবং অনেক কম কোডে।

নিচে কিছু উদাহরণ দেওয়া হলো যেখানে Java 8 এর Date and Time API ব্যবহার করা হয়েছে:

১. Get Current Date and Time

এখানে বর্তমান তারিখ এবং সময় পাওয়ার উদাহরণ:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;

public class CurrentDateTimeExample {
    public static void main(String[] args) {
        // বর্তমান তারিখ
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current Date: " + currentDate);
        
        // বর্তমান সময়
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Time: " + currentTime);
        
        // বর্তমান তারিখ এবং সময়
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date and Time: " + currentDateTime);
    }
}

Output:

Current Date: 2024-12-23
Current Time: 10:30:15.123456
Current Date and Time: 2024-12-23T10:30:15.123456

২. Create Specific Date and Time

নির্দিষ্ট তারিখ এবং সময় তৈরি করার উদাহরণ:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;

public class SpecificDateTimeExample {
    public static void main(String[] args) {
        // নির্দিষ্ট তারিখ তৈরি (yyyy-mm-dd)
        LocalDate specificDate = LocalDate.of(2024, 12, 25);
        System.out.println("Specific Date: " + specificDate);
        
        // নির্দিষ্ট সময় তৈরি (HH:mm:ss)
        LocalTime specificTime = LocalTime.of(14, 30, 0);
        System.out.println("Specific Time: " + specificTime);
        
        // নির্দিষ্ট তারিখ এবং সময় তৈরি
        LocalDateTime specificDateTime = LocalDateTime.of(2024, 12, 25, 14, 30, 0);
        System.out.println("Specific Date and Time: " + specificDateTime);
    }
}

Output:

Specific Date: 2024-12-25
Specific Time: 14:30:00
Specific Date and Time: 2024-12-25T14:30:00

৩. Format Date and Time

তারিখ এবং সময় ফরম্যাট করার উদাহরণ:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatDateTimeExample {
    public static void main(String[] args) {
        // বর্তমান তারিখ
        LocalDate currentDate = LocalDate.now();
        
        // একটি নির্দিষ্ট ফরম্যাট তৈরি করা
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        
        // তারিখ ফরম্যাট করা
        String formattedDate = currentDate.format(formatter);
        System.out.println("Formatted Date: " + formattedDate);
    }
}

Output:

Formatted Date: 23-12-2024

৪. Add and Subtract Days, Months, Years

তারিখে দিন, মাস, বা বছর যোগ বা বিয়োগ করার উদাহরণ:

import java.time.LocalDate;

public class AddSubtractDateExample {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        
        // 5 দিন যোগ করা
        LocalDate newDatePlusDays = currentDate.plusDays(5);
        System.out.println("Date after 5 days: " + newDatePlusDays);
        
        // 2 মাস বিয়োগ করা
        LocalDate newDateMinusMonths = currentDate.minusMonths(2);
        System.out.println("Date before 2 months: " + newDateMinusMonths);
        
        // 1 বছর যোগ করা
        LocalDate newDatePlusYears = currentDate.plusYears(1);
        System.out.println("Date after 1 year: " + newDatePlusYears);
    }
}

Output:

Date after 5 days: 2024-12-28
Date before 2 months: 2024-10-23
Date after 1 year: 2025-12-23

৫. Calculate Duration Between Two Dates

দুইটি তারিখের মধ্যে সময়ের পার্থক্য বের করার উদাহরণ:

import java.time.LocalDate;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

public class DurationExample {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2024, 1, 1);
        LocalDate endDate = LocalDate.now();
        
        // তারিখের মধ্যে দিন সংখ্যা বের করা
        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
        System.out.println("Days between: " + daysBetween);
        
        // সময়ের মধ্যে Duration বের করা (ঘণ্টায়)
        Duration duration = Duration.between(startDate.atStartOfDay(), endDate.atStartOfDay());
        System.out.println("Duration in hours: " + duration.toHours());
    }
}

Output:

Days between: 358
Duration in hours: 8592

৬. Period Example (Years, Months, Days)

দুইটি তারিখের মধ্যে বছর, মাস, দিন বের করার উদাহরণ:

import java.time.LocalDate;
import java.time.Period;

public class PeriodExample {
    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2020, 5, 15);
        LocalDate endDate = LocalDate.now();
        
        // তারিখের মধ্যে Period বের করা (বছর, মাস, দিন)
        Period period = Period.between(startDate, endDate);
        System.out.println("Period: " + period.getYears() + " years, " + period.getMonths() + " months, " + period.getDays() + " days");
    }
}

Output:

Period: 4 years, 7 months, 8 days

৭. ZonedDateTime Example

একটি নির্দিষ্ট টাইমজোনের তারিখ এবং সময় দেখানোর উদাহরণ:

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        // ZonedDateTime তৈরি (এটা UTC টাইমজোনের সময় দেখাবে)
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
        System.out.println("Current UTC Time: " + zonedDateTime);
        
        // একটি নির্দিষ্ট টাইমজোনের ZonedDateTime
        ZonedDateTime zonedDateTimeInNY = ZonedDateTime.now(ZoneId.of("America/New_York"));
        System.out.println("Current Time in New York: " + zonedDateTimeInNY);
    }
}

Output:

Current UTC Time: 2024-12-23T15:30:45.123456Z[UTC]
Current Time in New York: 2024-12-23T10:30:45.123456-05:00[America/New_York]

Java 8 Date and Time API Overview:

  1. LocalDate: শুধুমাত্র তারিখ (বছর, মাস, দিন)।
  2. LocalTime: শুধুমাত্র সময় (ঘণ্টা, মিনিট, সেকেন্ড)।
  3. LocalDateTime: তারিখ এবং সময় একত্রিত।
  4. ZonedDateTime: টাইমজোনসহ তারিখ এবং সময়।
  5. Instant: একটি নির্দিষ্ট মুহূর্ত (UTC টাইমজোনে)।
  6. Duration: সময়ের পার্থক্য (সেকেন্ড, মিনিট, ঘণ্টা)।
  7. Period: তারিখের পার্থক্য (বছর, মাস, দিন)।

এই উদাহরণগুলির মাধ্যমে আপনি Java 8 এর নতুন Date and Time API ব্যবহার করে বিভিন্ন তারিখ ও সময় সম্পর্কিত কাজ করতে পারবেন।

Content added By
Promotion

Are you sure to start over?

Loading...