- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Display current Date and Time
In this article, we will understand how to display current Date and Time. Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.
Below is a demonstration of the same −
Suppose our input is −
Run the program
The desired output would be −
The current date and time is: 2022/03/17 23:43:17
Algorithm
Step 1 - START Step 2 - Declare an object of LocalDateTime namely date. Step 3 - Define the values. Step 4 - Define a date time format using DateTimeFormatter objects Step 5 - Display the date and time using a specific formats Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; public class Demo { public static void main(String[] args) { System.out.println("The required packages have been imported"); System.out.println("A LocalDateTime object has been defined"); DateTimeFormatter date_time = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); System.out.println("\nThe current date and time is: " +date_time.format(now)); } }
Output
The required packages have been imported A LocalDateTime object has been defined The current date and time is: 2022/03/17 23:43:17
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; public class Demo { static void Date_time(DateTimeFormatter date_time){ LocalDateTime now = LocalDateTime.now(); System.out.println("\nThe current date and time is: " +date_time.format(now)); } public static void main(String[] args) { System.out.println("The required packages have been imported"); System.out.println("A LocalDateTime object has been defined"); DateTimeFormatter date_time = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); Date_time(date_time); } }
Output
The required packages have been imported A LocalDateTime object has been defined The current date and time is: 2022/03/29 08:55:28
- Related Articles
- Java Program to Get Current Date/Time
- Java Program to display date and time in Milliseconds
- Java Program to display date and time information in lowercase
- Java Program to display date and time information in uppercase
- Java Program to display Current Time in another Time Zone
- C++ Program to print current Day, Date and Time
- How to get the current date and time in Java?
- Java Program to parse date and time
- Display nanoseconds with Java Date and Time Conversion Character
- Java Program to get current date, year and month
- How do I display the current date and time in an iOS application?
- How do I display the current date and time in an Android application?
- Current Date and Time in Perl
- Display complete date and time information using Formatter in Java
- Display entire date time with milliseconds in Java

Advertisements