Computer >> Máy Tính >  >> Lập trình >> Java

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

Làm thế nào để lấy ngày và giờ hiện tại trong Java? Trong hướng dẫn này, chúng ta sẽ xem xét ba phương pháp khác nhau trong Java 8.

Các lớp được sử dụng để hiển thị ngày và giờ là LocalDate , LocalTime , LocalDateTime .

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

LocalDate lớp được sử dụng để đại diện cho một ngày.

GetCurrentDate.java

import java.time.LocalDate;

public class GetCurrentDate {

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

Đầu ra:

2020-02-07

Ngày được định dạng

Chúng ta có thể sử dụng DateTimeFormatter lớp để định dạng hiển thị ngày. Ví dụ:hiển thị ngày hiện tại ở định dạng yyyy/mm/dd chúng tôi sử dụng:

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")));
    }
}

Đầu ra:

2020/02/07

LocalDate lớp có các phương thức hữu ích khác mà chúng ta có thể sử dụng để biết thêm chi tiết về ngày hiện tại, chẳng hạn như: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());
    }
}

Đầu ra:

Today's date: 2020-02-07
Day of week: FRIDAY
Day of month: 7
Day of year: 38

Lấy Giờ Hiện tại

LocalTime lớp đại diện cho một thời gian.

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")));
    }
}
Lưu ý:Chúng tôi có thể sử dụng DateTimeFormatter để định dạng hiển thị thời gian.

Đầu ra:

Time now: 00:02:53.313
Formatted time: 00:02:53

LocalTime lớp cũng có một số phương thức tiện ích hữu ích để biết thêm chi tiết về thời gian hiện tại:

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());
    }
}

Đầu ra:

Current hour: 0
Current minute: 10
Current second: 16

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

Để lấy ngày hiện tại thời gian, chúng ta có thể sử dụng LocalDateTime lớp học

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());
    }
}

Đầu ra:

2020/02/08 - 00:18:12
Day of month: 8
Current hour: 0