Cách lấy thời gian hiện tại trong Java
Trong bài viết này chúng ta sẽ tìm hiểu cách lấy thời gian hiện tại trong Java. Mình sẽ thực hiện hiển thị thời gian hiện tại bằng nhiều định dạng khác nhau trong Java.

Để hiểu được bài viết này, các bạn cần có kiến thức cơ bản sau:
- Chuỗi trong Java.
- Nhập xuất trong Java.
1. Lấy thời gian hiện tại Java ở định dạng default (mặc định)
Ở ví dụ này mình sử dụng class java.time.LocalDateTime để định dạng cho ngày giờ hiện tại.
Để lấy ngày giờ hiện tại ta sử dụng phương thức now() trong class LocalDateTime: LocalDateTime.now()
Bài viết này được đăng tại [free tuts .net]
import java.time.LocalDateTime;
class Main {
public static void main(String[] args) {
//khai báo một đổi tượng current thuộc class LocalDateTime
LocalDateTime current = LocalDateTime.now();
// hiển thị ngày giờ hiện tại ra màn hình
System.out.println("\n\nNgày giờ hiện tại: " + current);
}
}Kết quả:
2. Lấy thời gian hiện tại Java ở định dạng pattern (mẫu)
Ở ví dụ này mình sẽ sử dụng class LocalDateTime và DateTimeFormatter, vì vậy hãy khai báo thư viện nhé:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;
Ngày và giờ hiện tại sẽ được định dạng theo mẫu:
Year-Month-Day Hours:Minutes:Seconds.Milliseconds. ví dụ: 2020-02-12 09:41:22.5124
Ta sử dụng class DateTimeFormatter.ofPattern để định dạng theo giống như mẫu trên.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");Full code:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
//khai báo đối tượng current thuộc class LocalDateTime
LocalDateTime current = LocalDateTime.now();
//sử dụng class DateTimeFormatter để định dạng ngày giờ theo kiểu pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
//sử dụng phương thức format() để định dạng ngày giờ hiện tại rồi gán cho chuỗi formatted
String formatted = current.format(formatter);
//hiển thị chuỗi formatted ra màn hình
System.out.println("\n\nNgày giờ hiện tại: " + formatted);
System.out.println("\n------------------------------------");
System.out.println("Chương trình này được đăng tại Freetuts.net");
}
}Kết quả:
3. Lấy thời gian hiện tại Java định dạng localized
Ở ví dụ này mình sử dụng class LocalDateTime, DateTimeFormatter và FormatStyle, vì vậy hãy khai báo thư viện nhé:
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle;
Ta sử dụng phương thức now() trong class LocalDateTime để lấy ngày giờ hiện tại.
LocalDateTime current = LocalDateTime.now();
Và sử dụng class class DateTimeFormatter kết hợp với class FormatStyle để định dạng ngày giờ theo dạng localized.
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
Full code:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
class Main {
public static void main(String[] args) {
//khai báo đối tượng curent thuộc class LocalDateTime
LocalDateTime current = LocalDateTime.now();
//sử dụng class DateTimeFormatter và class FormatStyle để định dạng ngày giờ hiện tại theo dạng localized
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
//gán ngày giờ sau khi format cho chuỗi formatted
String formatted = current.format(formatter);
System.out.println("\n\nNgày giờ hiện tại: " + formatted);
System.out.println("\n------------------------------------");
System.out.println("Chương trình này được đăng tại Freetuts.net");
}
}Kết quả:
Như vậy là chúng ta đã tìm hiểu xong cách lấy thời gian hiện tại Java bằng nhiều cách format khác nhau. Các bạn có thể tìm hiểu về các bài tập Java, chúc các bạn thành công !!!

Các kiểu dữ liệu trong C ( int - float - double - char ...)
Thuật toán tìm ước chung lớn nhất trong C/C++
Cấu trúc lệnh switch case trong C++ (có bài tập thực hành)
ComboBox - ListBox trong lập trình C# winforms
Random trong Python: Tạo số random ngẫu nhiên
Lệnh cin và cout trong C++
Cách khai báo biến trong PHP, các loại biến thường gặp
Download và cài đặt Vertrigo Server
Thẻ li trong HTML
Thẻ article trong HTML5
Cấu trúc HTML5: Cách tạo template HTML5 đầu tiên
Cách dùng thẻ img trong HTML và các thuộc tính của img
Thẻ a trong HTML và các thuộc tính của thẻ a thường dùng