Java.Java8.How to get current date using Date Time API from Java 8?

✅ Ways to Get the Current Date

🔹 1. Using LocalDate

If you want just the date (without time):

import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current date: " + currentDate);
    }
}

🔸 Output:

Current date: 2025-04-15

🔹 2. Using LocalDateTime

If you want date + time, but no time zone:

import java.time.LocalDateTime;

LocalDateTime now = LocalDateTime.now();
System.out.println("Date & Time: " + now);

🔹 3. Using ZonedDateTime

If you need date + time + time zone:

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

ZonedDateTime zonedNow = ZonedDateTime.now(); // system default zone
System.out.println("Zoned DateTime: " + zonedNow);

Or use a specific time zone:

ZonedDateTime tokyoNow = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
System.out.println("Tokyo DateTime: " + tokyoNow);

🧠 Bonus: Get Date in a Specific Format

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

LocalDate date = LocalDate.now();
String formatted = date.format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
System.out.println("Formatted: " + formatted);
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.