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:

Phương thức tham chiếu trong Java 8 – Method References
Query Entities by Dates and Times with Spring Data JPA
Spring Security Basic Authentication
Java Program to Implement Circular Doubly Linked List
Java Program to Implement Sparse Matrix
Java Program to Construct an Expression Tree for an Infix Expression
Guide to Spring Cloud Kubernetes
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Bootstrap a Web Application with Spring 5
Java Program to Implement Rope
Case-Insensitive String Matching in Java
Annotation trong Java 8
OAuth2 Remember Me with Refresh Token
Java Program to Find the GCD and LCM of two Numbers
Java Program to Implement Floyd-Warshall Algorithm
Working with Network Interfaces in Java
Java Program to Find Basis and Dimension of a Matrix
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Java Program to Implement Floyd Cycle Algorithm
A Guide to the ResourceBundle
Java Program to Check whether Graph is Biconnected
Quick Intro to Spring Cloud Configuration
New Features in Java 9
New Features in Java 14
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
“Stream has already been operated upon or closed” Exception in Java
Java Program for Douglas-Peucker Algorithm Implementation
Spring Boot - Sending Email
Java Program to Implement a Binary Search Tree using Linked Lists
How to Use if/else Logic in Java 8 Streams
Sort a HashMap in Java