
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to get the current date and time in Java?
You can get the current date and time in Java in various ways. Following are some of them −
The constructor of Date class
The no-arg constructor of the java.util.Date class returns the Date object representing the current date and time.
Example
import java.util.Date; public class CreateDate { public static void main(String args[]) { Date date = new Date(); System.out.print(date); } }
Output
Thu Nov 05 20:04:57 IST 2020
The now() method of LocalDateTime class
The now() method of the LocaldateTime class returns the Date object representing the current time.
Example
import java.time.LocalDate; import java.time.LocalDateTime; public class CreateDateTime { public static void main(String args[]) { LocalDateTime date = LocalDateTime.now(); System.out.println("Current Date and Time: "+date); } }
Output
Current Date and Time: 2020-11-05T21:49:11.507
The getInstance() method of the calendar class
The getInstance() (without arguments) method of the this class returns the Calendar object representing the current date and time.
Example
import java.util.Calendar; import java.util.Date; public class CreateDateTime { public static void main(String[] args) { Calendar obj = Calendar.getInstance(); Date date = obj.getTime(); System.out.println("Current Date and time: "+date); } }
Output
Current Date and time: Thu Nov 05 21:46:19 IST 2020
The java.time.Clock class
You can also get the current date ant time value using the java.time.Clock class as shown below −
Example
import java.time.Clock; public class CreateDateTime { public static void main(String args[]) { Clock obj = Clock.systemUTC(); System.out.println("Current date time: "+obj.instant()); } }
Output
Current date time: 2020-11-05T16:33:06.155Z
- Related Articles
- Java Program to Get Current Date/Time
- How to get current time and date in C++?
- How to get current date and time in Python?
- How to get current date and time in JavaScript?
- How to get current date/time in milli seconds in Java?
- How to get the current local date and time in Kotlin?
- How to get current date and time using JavaScript?
- How to get the current date in Java?
- How to get current date and time from internet in iOS?
- How to get current date and time from internet in android?
- How can I get the current time and date in Kotlin?
- How do I get the current date and time in JavaScript?
- Get current time and date on Android
- How to get current date/time in seconds in JavaScript?
- Java Program to Display current Date and Time

Advertisements