
- 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 in Java?
You can get the current date 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, using this you can print the current date as shown below −
Example
import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; public class Demo { public static void main(String args[])throws ParseException { Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy"); String str = formatter.format(date); System.out.print("Current date: "+str); } }
Output
05/11/20
The now() method of LocalDate class
The now() method of the Localdate class returns the Date object representing the current time.
Example
import java.time.LocalDate; public class CreateDate { public static void main(String args[]) { LocalDate date = LocalDate.now(); System.out.println("Current Date: "+date); } }
Output
Current Date: 2020-11-05
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, using this you can print the current date value as shown below −
Example
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Calendar; public class Test { public static void main(String[] args) throws ParseException{ DateFormat formatter = new SimpleDateFormat("dd/MM/yy"); Calendar obj = Calendar.getInstance(); String str = formatter.format(obj.getTime()); System.out.println("Current Date: "+str ); } }
Output
Current Date: 05/11/20
The java.sql.Date class
One of the constructor of the java.sql.Date class accepts a long value representing a date and creates a Date object. Therefore to create a Data object you need to pass the return value of the System.currentTimeMillis() method (returns current epoch value) as a parameter of the java.sql.Date constructor.
Example
public class CreateDate { public static void main(String[] args) { java.sql.Date date=new java.sql.Date(System.currentTimeMillis()); System.out.println("Current Date: "+date); } }
Output
Current Date: 2020-11-05
- Related Articles
- How to get the current date and time in Java?
- Java Program to Get Current Date/Time
- How to get current date/time in milli seconds in Java?
- Java Program to get current date, year and month
- How to get the difference between date records and the current date in MySQL?
- How to get current date and time in Python?
- How to get current date and time in JavaScript?
- How to get current time and date in C++?
- How do I get the current date in JavaScript?
- How to get the current local date and time in Kotlin?
- How to get current date/time in seconds in JavaScript?
- How to get the current date to display in a Tkinter window?
- Java Program to get date for all the days of the current week
- How to get current date and time using JavaScript?
- Tkinter-How to get the current date to display in a tkinter window?
