Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Java 8: Lấy ngày và giờ hiện tại với LocalDate, LocalTime, LocalDateTime

Làm thế nào để lấy ngày và giờ hiện tại trong Java? Hướng dẫn này trình bày 3 phương pháp trong Java 8.

Các lớp dùng để hiển thị ngày giờ: LocalDate, LocalTime, LocalDateTime.

Lấy ngày hiện tại

Lớp LocalDate đại diện cho ngày (không có giờ).

import java.time.LocalDate;

public class GetCurrentDate {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println(now.toString());
    }
}

Kết quả:

2020-02-07

Định dạng ngày

Sử dụng DateTimeFormatter để định dạng hiển thị. Ví dụ định dạng 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")));
    }
}

Kết quả:

2020/02/07

Lớp LocalDate còn cung cấp các phương thức hữu ích: getDayOfWeek(), getDayOfMonth(), getDayOfYear():

import java.time.LocalDate;

public class GetCurrentDate {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println("Ngày hôm nay: " + now.toString());
        System.out.println("Thứ trong tuần: " + now.getDayOfWeek());
        System.out.println("Ngày trong tháng: " + now.getDayOfMonth());
        System.out.println("Ngày trong năm: " + now.getDayOfYear());
    }
}

Kết quả:

Ngày hôm nay: 2020-02-07
Thứ trong tuần: FRIDAY
Ngày trong tháng: 7
Ngày trong năm: 38

Lấy giờ hiện tại

Lớp LocalTime đại diện cho giờ (không có ngày).

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("Giờ hiện tại: " + now.toString());
        System.out.println("Giờ định dạng: " + now.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
    }
}

Lưu ý: Có thể dùng DateTimeFormatter để định dạng giờ.

Kết quả:

Giờ hiện tại: 00:02:53.313
Giờ định dạng: 00:02:53

LocalTime cũng có các phương thức tiện ích: getHour(), getMinute(), getSecond():

import java.time.LocalTime;

public class GetCurrentTime {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        System.out.println("Giờ: " + now.getHour());
        System.out.println("Phút: " + now.getMinute());
        System.out.println("Giây: " + now.getSecond());
    }
}

Kết quả:

Giờ: 0
Phút: 10
Giây: 16

Lấy ngày và giờ hiện tại

Để lấy cả ngày và giờ, dùng LocalDateTime:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GetCurrentDateTime {
    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("Ngày trong tháng: " + now.getDayOfMonth());
        System.out.println("Giờ hiện tại: " + now.getHour());
    }
}

Kết quả:

2020/02/08 - 00:18:12
Ngày trong tháng: 8
Giờ hiện tại: 0