Java SimpleDateFormat

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The java.text.SimpleDateFormat class formats and parses dates in Java, extending DateFormat. It supports custom date patterns and locales, enabling conversion between Date objects and their string representations.

Declaration

We can declare the SimpleDateFormat class in the following way. It extends the SimpleDateFormat class and it contains all the methods of the base class along with its own.

Constructors of the Class SimpleDateFormat

Sr. NoConstructorsDescriptionExamples
1SimpleDateFormat()Constructs a SimpleDateFormat using the default pattern and date format symbols for the default locale.SimpleDateFormat p = new SimpleDateFormat();
2SimpleDateFormat(String pattern)Constructs a SimpleDateFormat using the specified pattern and the default date format symbols for the default locale.SimpleDateFormat p = new SimpleDateFormat("yyyy MM dd");
3SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)Constructs a SimpleDateFormat using the specified pattern and date format symbols.DateFormatSymbols symbols = new DateFormatSymbols(new Locale("en", "US")); symbols.setAmPmStrings(new String[] {"AM", "PM"}); SimpleDateFormat p = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a", symbols);
4SimpleDateFormat(String pattern, Locale locale)Constructs a SimpleDateFormat using the specified pattern and the default date format symbols for the given locale.SimpleDateFormat p = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Java SimpleDateFormat Examples

Example 1: Date to String

This example demonstrates formatting a date as a string using SimpleDateFormat.

File: FormatDate.java

Example 2: String to Date

This example shows how to parse a date string back into a Date object with SimpleDateFormat.

File: ParseDate.java

Read more about converting strings to dates in Java on Scaler.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Methods Summary

Sr. NoMethodReturn data-typeDescription
1.parse()DateThis method is used to parse text from a string to form and return the Date.
2.set2DigitYearStart(Date startDate)voidThis method is used to parse the date and set the date in the range startDate to startDate + 100 years.
3.get2DigitYearStart()DateThis method is used to get the start of a period of 100 years set by the user during parsing.
4.toLocalizedPattern()StringThis method is used to return the date pattern String used in the formatter.
5.format(Date date)StringThis method is used to convert a Date to a string.
6.applyPattern(String pattern)voidThis method is used to set the given pattern to the Date Format.
7.toPattern()StringThis method is used to return a pattern describing the string.

Methods in Detail

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

1. parse()

  • Return type- Date
  • Description- This method is used to parse text from a string to form and return the Date.
  • Parameters- None

Example-

Output-

The above code demonstrates the use of the parse() function. We have created a SimpleDateFormat MM/dd/yy format, created a Calender instance parsed the string, and printed the date and time. Any error occurring is caught in the catch block.

2. set2DigitYearStart(Date StartDate)

  • Return type- void
  • Description- This method is used to parse the date and set the date in the range startDate to startDate + 100 years.
  • Parameters- Date startDate

Example-

Output-

The above code demonstrates the use of set2DigitYearStar function. We have created a SimpleDateFormat MM/dd/yy format, created a Calender instance, and set a different year using set2DigitYearStart() function and printed the date and time. Any error occurring is caught in the catch block.

3. get2DigitYearStart()

  • Return type- Date
  • Description- This method is used to get the start of a period of 100 years set by the user during parsing.
  • Parameter- None

Example-

Output-

The above code demonstrates the use of get2DigitYearStart() function. We have created a SimpleDateFormat MM/dd/yy format, created a Calender instance set a year using set2DigitYearStart() function, and printed the date and time. As the get2DigitYearStart() checks and returns the year set by the user, we get the year that was set by us previously. Any error occurring is caught in the catch block.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

4. toLocalizedPattern()

  • Return type- String
  • Description- This method is used to return the date pattern String used in the formatter.
  • Parameter- None

Example-

Output-

The above code demonstrates the use of toLocalizedPattern function. We have created an object using the default constructor and then created a calendar instance. The current date and time are printed and we used the format function to format the Date to a String and then printed the same.

5. Format(Date date)

  • Return type- String
  • Description- This method is used to convert a Date to a string.
  • Parameter- Date date

Example-

Output-

The above code demonstrates the use of the format function. We have created a SimpleDateFormat and created an instance of a calendar object and then formatted the current time and displayed it.

6. applyPattern(String Pattern)

  • Return type- void
  • Description- This method is used to set the given pattern to the Date Format.
  • Parameter- String pattern

Example-

Output-

The above code demonstrates the use of applyPattern() function. We have created a SimpleDateFormat and created an instance of a calendar object. Now as we want to apply a pattern, we first define it and pass it as an argument to the applyPattern function. The current date and the pattern are displayed to the user.

7. toPattern()

  • Return type- String
  • Description- This method is used to return a pattern describing the string.
  • Parameters- None

Example-

Output-

The above code demonstrates the use of toPattern() function. We have created a SimpleDateFormat and created an instance of a calendar object. Now we want to apply a pattern, we first define it and pass it as an argument to the applyPattern function. We then print the pattern that has been applied earlier using the toPattern function. The current date and the pattern are displayed to the user.

Conclusion

  • The Java SimpleDateFormat class transforms dates into readable strings and vice versa, bridging the gap between textual and date formats.
  • It's a concrete utility for handling date-time representations, customizable with patterns, locales, and time zones.
  • With various constructors and methods, SimpleDateFormat offers flexibility in date formatting and parsing operations.
  • Practical examples demonstrate its ease of use and adaptability, from straightforward formatting to complex locale-specific conversions.