![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
How to use the SimpleDateFormat class to convert a Java Date to a formatted String?
The Java SimpleDateFormat class provides convert between a Java String to a Date or Date to String.
Example
import java.util.Date; import java.text.SimpleDateFormat; import java.util.Calendar; public class SimpleDateFormatTest { public static void main(String[] args) { // get today's date Date today = Calendar.getInstance().getTime(); // create a date "formatter" SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss"); // create a new String using the date format String formattedDate = formatter.format(today); // prints converted date to string format System.out.println(" Date is:" + formattedDate); } }
Output
Date is:2023-11-21-10.39.16
Advertisements