Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
DateFormat format() Method in Java with Examples
Next article icon

DateFormat format() Method in Java with Examples

Last Updated : 22 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

DateFormat class present inside java.text package is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time. DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner. 

The format() method of DateFormat class in Java is used to format a given date into a Date/Time string. Basically, the method is used to convert this date and time into a particular format i.e., mm/dd/yyyy.

Syntax: 

public final String format(Date date)

Parameters: The method takes one parameter date of the Date object type and refers to the date whose string output is to be produced.

Return Type: Returns Date or time in string format of mm/dd/yyyy.

Example 1:

Java
// Java Program to Illustrate format() Method // of DateTime Class  // Importing required classes import java.text.*; import java.util.Calendar;  // Main class // DateFormat_Demo public class GFG {      // Main driver method     public static void main(String[] args)     {          // Initializing the first formatter         DateFormat DFormat = DateFormat.getDateInstance();          // Initializing the calendar Object         Calendar cal = Calendar.getInstance();          // Displaying the actual date         System.out.println("The original Date: "                            + cal.getTime());          // Converting date using format() method         String curr_date = DFormat.format(cal.getTime());          // Printing the formatted date         System.out.println("Formatted Date: " + curr_date);     } } 

Output: 
The original Date: Wed Mar 27 11:12:29 UTC 2019 Formatted Date: Mar 27, 2019

 

Example 2:

Java
// Java Program to Illustrate format() Method // of DateTime Class  // Importing required classes import java.text.*; import java.util.*;  // Main class // DateFormat_Demo public class GFG {      // Main driver method     public static void main(String[] args)     {         // Initializing the first formatter         DateFormat DFormat = DateFormat.getDateTimeInstance(             DateFormat.LONG, DateFormat.LONG,             Locale.getDefault());          // Initializing the calendar Object         Calendar cal = Calendar.getInstance();          // Displaying the actual date         System.out.println("The original Date: "                            + cal.getTime());          // Converting date using format() method and         // storing date in a string         String curr_date = DFormat.format(cal.getTime());          // Printing the formatted date on console         System.out.println("Formatted Date: " + curr_date);     } } 

Output
The original Date: Tue Jan 11 05:42:29 UTC 2022 Formatted Date: January 11, 2022 at 5:42:29 AM UTC

Next Article
DateFormat format() Method in Java with Examples

C

chinmoy lenka
Improve
Article Tags :
  • Misc
  • Java
  • Java - util package
  • Java-Functions
  • Java-DateFormat
Practice Tags :
  • Java
  • Misc

Similar Reads

    ChoiceFormat format() method in Java with Examples
    The format() method of java.text.ChoiceFormat class is used to get the appended string builder of the format value of particular limit value passed as parameter and text passed as parameter in this method. Syntax: public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition statu
    2 min read
    Date from() method in Java with Examples
    The from(Instant inst) method of Java Date class returns an instance of date which is obtained from an Instant object. Syntax: public static Date from(Instant inst) Parameters: The method takes one parameter inst of Instant type which is required to be converted. Return Value: The method returns a d
    2 min read
    DateFormat hashCode() method in Java with Examples
    The hashCode() Method of DateFormat class is used to return the hash code value of this DateFormat object. The hash code is of integer type. Syntax: public int hashCode() Parameters: The method does not take any parameters. Return Value: The method returns the hash code value of this DateFormat obje
    2 min read
    Java DateFormat getInstance() Method with Examples
    The getInstance() method of DateFormat class will return the date and time formatter with the default formatting style for the default locale. Syntax: public static final DateFormat getInstance() Parameter: This method doesn't require any parameters. Return value: This method will return the date fo
    2 min read
    DateFormat getCalendar() Method in Java with Examples
    The getCalendar() Method of DateFormat class in Java is used to get the calendar associated with this date/time format object. Syntax: public Calendar getCalendar() Parameter: The method does not take any parameters. Return Value: The method returns an instance of Calendar for this DateFormat object
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences