

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 format date with SimpleDateFormat
The SimpleDateFormat class is used to parse and format dates. To create an object, firstly import the following package
import java.text.SimpleDateFormat;
Set the date
String strDate = "'Year = '"; strDate += "yyyy"; strDate += "'\nMonth = '"; strDate += "MMMMMMMMM"; strDate += "'\nHours = '"; strDate += "hh"; strDate += "a"; strDate += "'\nMinutes = '"; strDate += "mm"; strDate += "'\nSeconds = '"; strDate += "ss";
Now, create an object and format the date −
SimpleDateFormat dateFormat = new SimpleDateFormat(strDate); String formattedDate = dateFormat.format(new Date());
The following is an example −
Example
import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String args[]) { String strDate = "'Year = '"; strDate += "yyyy"; strDate += "'\nMonth = '"; strDate += "MMMMMMMMM"; strDate += "'\nHours = '"; strDate += "hh"; strDate += "a"; strDate += "'\nMinutes = '"; strDate += "mm"; strDate += "'\nSeconds = '"; strDate += "ss"; System.out.println("Date and Time...."); SimpleDateFormat dateFormat = new SimpleDateFormat(strDate); String formattedDate = dateFormat.format(new Date()); System.out.println(formattedDate); } }
Output
Date and Time.... Year = 2018 Month = November Hours = 09AM Minutes = 12 Seconds = 41
- Related Questions & Answers
- Format date with SimpleDateFormat('MM/dd/yy') in Java
- How to Format a Date String using SimpleDateFormat in Java?
- Java Program to format date with DateFormat.FULL
- Java Program to format date with System.out.format
- Java Program to format date time with Join
- Set Date patterns with SimpleDateFormat in Java
- How to format year using SimpleDateFormat in Java?
- Java Program to parse string date value with default format
- Display today’s date in Java with SimpleDateFormat
- What are SimpleDateFormat Format Codes in Java?
- Format date with DateFormat.SHORT in Java
- Format Date with getDateTimeInstance() in Java
- Format date with DateFormat.MEDIUM in Java
- Format date with DateFormat.LONG in Java
- Java Program to display date with day name in short format
Advertisements