- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Display today’s date in Java with SimpleDateFormat
To work with SimpleDateFormat class in Java, import the following package.
import java.text.SimpleDateFormat;
To get the date, use the format() method as shown below. Firstly, set the date format −
Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MM/yyyy");
Now, we will get the date −
simpleformat.format(cal.getTime())
The following is the complete example −
Example
import java.text.SimpleDateFormat; import java.util.Calendar; public class Demo { public static void main(String[] args) throws Exception { Calendar cal = Calendar.getInstance(); SimpleDateFormat simpleformat = new SimpleDateFormat("dd/MM/yyyy"); System.out.println("Today's date = "+simpleformat.format(cal.getTime())); } }
Output
Today's date = 26/11/2018
Advertisements