জাভাতে বর্তমান তারিখ ও সময় কীভাবে পাওয়া যায়? এই টিউটোরিয়ালে আমরা জাভা ৮-এর তিনটি ভিন্ন পদ্ধতি দেখব।
তারিখ ও সময় প্রদর্শনের জন্য ব্যবহৃত ক্লাসগুলো হল LocalDate, LocalTime, LocalDateTime।
বর্তমান তারিখ পেতে
LocalDate ক্লাস একটি তারিখ উপস্থাপন করতে ব্যবহৃত হয়।
GetCurrentDate.java
import java.time.LocalDate;
public class GetCurrentDate {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now.toString());
}
}
আউটপুট:
2020-02-07
ফরম্যাট করা তারিখ
তারিখের প্রদর্শন ফরম্যাট করতে DateTimeFormatter ক্লাস ব্যবহার করা যায়। উদাহরণস্বরূপ, বর্তমান তারিখ yyyy/mm/dd ফরম্যাটে দেখানোর জন্য:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class GetCurrentDate {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
}
}
আউটপুট:
2020/02/07
LocalDate ক্লাসের আরও কিছু উপযোগী মেথড আছে যা বর্তমান তারিখ সম্পর্কে বিস্তারিত জানতে সাহায্য করে: getDayOfWeek(), getDayOfMonth(), getDayOfYear()
import java.time.LocalDate;
public class GetCurrentDate {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println("Today's date: " + now.toString());
System.out.println("Day of week: " + now.getDayOfWeek().toString());
System.out.println("Day of month: " + now.getDayOfMonth());
System.out.println("Day of year: " + now.getDayOfYear());
}
}
আউটপুট:
Today's date: 2020-02-07
Day of week: FRIDAY
Day of month: 7
Day of year: 38
বর্তমান সময় পেতে
LocalTime ক্লাস একটি সময় উপস্থাপন করে।
GetCurrentTime.java
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class GetCurrentTime {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println("Time now: " + now.toString());
System.out.println("Formatted time: " + now.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
}
}
দ্রষ্টব্য: সময়ের প্রদর্শন ফরম্যাট করার জন্য DateTimeFormatter ব্যবহার করা যেতে পারে।
আউটপুট:
Time now: 00:02:53.313
Formatted time: 00:02:53
LocalTime ক্লাসের কিছু হ্যান্ডি ইউটিলিটি মেথড আছে বর্তমান সময় সম্পর্কে বিস্তারিত জানতে:
import java.time.LocalTime;
public class GetCurrentTime {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
System.out.println("Current hour: " + now.getHour());
System.out.println("Current minute: " + now.getMinute());
System.out.println("Current second: " + now.getSecond());
}
}
আউটপুট:
Current hour: 0
Current minute: 10
Current second: 16
বর্তমান তারিখ ও সময় পেতে
বর্তমান তারিখ এবং সময় পেতে আমরা LocalDateTime ক্লাস ব্যবহার করতে পারি
package io.devqa.tutorials;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class GetCurrentTime {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd - HH:mm:ss")));
System.out.println("Day of month: " + now.getDayOfMonth());
System.out.println("Current hour: " + now.getHour());
}
}
আউটপুট:
2020/02/08 - 00:18:12
Day of month: 8
Current hour: 0