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

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:

Performance Difference Between save() and saveAll() in Spring Data
Jackson JSON Views
Concrete Class in Java
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Permute All Letters of an Input String
The HttpMediaTypeNotAcceptableException in Spring MVC
Vòng lặp for, while, do-while trong Java
Guide to System.gc()
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Recommended Package Structure of a Spring Boot Project
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Jackson vs Gson
Converting String to Stream of chars
Cơ chế Upcasting và Downcasting trong java
Spring Security OAuth Login with WebFlux
Spring Data Java 8 Support
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Check If Two Lists are Equal in Java
Java Program to Represent Graph Using Adjacency List
Introduction to Spring Cloud CLI
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Mapping Nested Values with Jackson
Spring Boot - Batch Service
How to Set TLS Version in Apache HttpClient
Java Program to Implement Lloyd’s Algorithm
Returning Image/Media Data with Spring MVC
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Sorting in Java
Hướng dẫn sử dụng Printing Service trong Java
An Intro to Spring Cloud Contract
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Check whether Graph is a Bipartite using DFS