W3Schools Java SimpleDateFormat Guide: Master Date Formatting Quickly

Robert Jul 13, 2026

Java's SimpleDateFormat, a part of the java.text package, is a powerful tool for date and time parsing, as well as formatting. It allows developers to express dates, times, and datetimes in various formats, and interpret them.

a computer screen with some type of text on it and numbers in the bottom left corner
a computer screen with some type of text on it and numbers in the bottom left corner

In this guide, we'll dive into using SimpleDateFormat in Java, its capabilities, and some practical examples.

Date And Time In Java – Importance, Local, Current, and Formatting
Date And Time In Java – Importance, Local, Current, and Formatting

Understanding SimpleDateFormat

SimpleDateFormat is a concrete class for formatting and parsing dates in a local (default) locale. It's versatile, allowing you to define your own format templates to cater to different date and time representations.

Java Data Types Explained
Java Data Types Explained

Here's how to create an instance of SimpleDateFormat:

Initialization

an image of a computer screen with many lines on the screen and numbers in different colors
an image of a computer screen with many lines on the screen and numbers in different colors

You instantiate SimpleDateFormat by passing the desired format pattern as a string to its constructor. This pattern determines how dates and times are parsed or formatted. For example:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Common Format Patterns

SimpleDateFormat supports various predefined pattern letters that could represent different date and time components. Some common ones include:

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
  • yyyy – year
  • MM – month in year
  • dd – day in month
  • yyyy-MM-dd – date
  • HH:mm:ss – time

Parsing Dates with SimpleDateFormat

SimpleDateFormat's parse() method converts a string to a Date object, following the format given when creating the object.

a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '

Here's a simple example:

String dateString = "2022-10-15 15:30:45"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse(dateString);

Implementing throws ParseException

Java Cheatsheet
Java Cheatsheet
Get Current Date and Time in Java
Get Current Date and Time in Java
Java Patterns Notes 1
Java Patterns Notes 1
📎 UNDERSTANDING JAVA ANNOTATIONS AND THEIR USES 🧩
📎 UNDERSTANDING JAVA ANNOTATIONS AND THEIR USES 🧩
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
Java
Java
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
java basics
java basics
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
Java Project Ideas For Beginners
Java Project Ideas For Beginners
Making Sense of Our First Java Program
Making Sense of Our First Java Program
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
30 Days of Java – Day 10 | Java Method Parameters Explained for Beginners
30 Days of Java – Day 10 | Java Method Parameters Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
Beginner's Guide to Learning Java
Beginner's Guide to Learning Java
Learn Java in 30 Days Only
Learn Java in 30 Days Only
30 Days of Java – Day 1 | What is Java? (Beginner Guide)
30 Days of Java – Day 1 | What is Java? (Beginner Guide)
Learn Java in 30 Days Only
Learn Java in 30 Days Only

As parse() might throw a ParseException when the string doesn't match the expected format, it's best to implement the throws clause either in the method declaration or use try-catch block.

Example with try-catch:

try { Date date = sdf.parse(dateString); } catch (ParseException e) { e.printStackTrace(); }

Formatting Dates with SimpleDateFormat

SimpleDateFormat's format() method converts a Date object to a string representing it as per the given format.

Here's how you'd format the date:

String formattedDate = sdf.format(date);

Formatting and Parsing Time Zones

SimpleDateFormat supports time zones through the SimpleTimeZone class. Here's how you can set the time zone:

sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

Always use the best-suited format patterns, and implement exception handling to ensure robust date parsing and formatting.

Now you understand how to handle dates and times effectively in Java with SimpleDateFormat.