Table of Contents
- 1. Lấy ngày hiện tại: java.time.LocalDate
- 2. Lấy giờ hiện tại: java.time.LocalTime
- 3. Lấy ngày và giờ hiện tại: java.time.LocalDateTime
- 4. Lấy ngày và giờ hiện tại: java.time.Clock
- 5. Lấy ngày và giờ hiện tại: java.util.Date
- 6. Lấy ngày hiện tại: java.sql.Date
- 7. Lấy ngày và giờ hiện tại: java.util.Calendar
Có nhiều cách để lấy ngày giờ hiện tại trong Java. Trong bài này, tôi sẽ giới thiệu một vài cách thường được sử dụng để lấy ngày giờ hiện tại.
1. Lấy ngày hiện tại: java.time.LocalDate
System.out.println(java.time.LocalDate.now());
Kết quả:
2017-01-23
2. Lấy giờ hiện tại: java.time.LocalTime
System.out.println(java.time.LocalTime.now());
Kết quả:
00:01:14.341
3. Lấy ngày và giờ hiện tại: java.time.LocalDateTime
System.out.println(java.time.LocalDateTime.now());
Kết quả:
2017-01-24T00:03:31.593
4. Lấy ngày và giờ hiện tại: java.time.Clock
System.out.println(java.time.Clock.systemUTC().instant());
Kết quả:
2017-01-23T18:35:23.669Z
5. Lấy ngày và giờ hiện tại: java.util.Date
java.util.Date date=new java.util.Date(); System.out.println(date);
hoặc
long millis=System.currentTimeMillis(); java.util.Date date=new java.util.Date(millis); System.out.println(date);
Kết quả:
Thu Oct 26 08:22:02 IST 2017
6. Lấy ngày hiện tại: java.sql.Date
long millis=System.currentTimeMillis(); java.sql.Date date=new java.sql.Date(millis); System.out.println(date);
Kết quả:
2017-10-26
7. Lấy ngày và giờ hiện tại: java.util.Calendar
Date date=java.util.Calendar.getInstance().getTime(); System.out.println(date);
Kết quả:
Thu Oct 26 08:22:02 IST 2017
Như bạn thấy có rất nhiều cách để lấy ngày giờ trong Java, nếu có thể hãy sử dụng các phương thức Date Time trong Java 8 : java.time.* sẽ tốt hơn nhé.5.0
Related posts:
Exception Handling in Java
Java Program to Implement Suffix Tree
Lớp Collectors trong Java 8
Sắp xếp trong Java 8
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Hướng dẫn Java Design Pattern – Memento
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java 8 Stream findFirst() vs. findAny()
Object cloning trong java
Hướng dẫn Java Design Pattern – Composite
LinkedHashSet trong java
Logout in an OAuth Secured Application
Converting a List to String in Java
Logout in an OAuth Secured Application
Java Program to Implement Hash Tables with Double Hashing
Registration with Spring Security – Password Encoding
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java Program to Implement the One Time Pad Algorithm
Spring Boot - Interceptor
An Intro to Spring Cloud Task
Deploy a Spring Boot App to Azure
Java Program to Perform Stooge Sort
A Guide to Apache Commons Collections CollectionUtils
Thao tác với tập tin và thư mục trong Java
Hamcrest Collections Cookbook
Spring Data JPA and Null Parameters
DistinctBy in the Java Stream API
Java Program to Implement Stack using Two Queues
Test a REST API with Java
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Login For a Spring Web App – Error Handling and Localization