
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is SimpleDateFormat in Java?
The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.
Parsing a date string
One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object
- Instantiate this class by passing desired format string.
- Parse the date string using the parse() method.
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { String date_string = "2007-25-06"; //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM"); //Parsing the given String to Date object Date date = formatter.parse(date_string); System.out.println("Date value: "+date); } }
Output
Date value: Mon Jun 25 00:00:00 IST 2007
Retrieving a pattern string
The toPattern() method of this class returns the pattern string representing the format of the current object.
Example
import java.text.ParseException; import java.text.SimpleDateFormat; public class Demo { public static void main(String args[]) throws ParseException { SimpleDateFormat obj = new SimpleDateFormat(); String pattern = obj.toPattern(); System.out.println(pattern); } }
Output
M/d/yy h:mm a
Parsing a date from text
The parse() method of this class accepts ParsePosition as a parameter along with date string and, parses date from a text.
Example
import java.text.ParseException; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { String text = "Marriage date of Samrat is 2007-25-06"; //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("yyyy-dd-MM"); //Parsing date from the given text ParsePosition pos = new ParsePosition(27); Date date = formatter.parse(text, pos); System.out.println("Date value: "+date); } }
Output
Date value: Mon Jun 25 00:00:00 IST 2007
- Related Articles
- What are SimpleDateFormat Format Codes in Java?
- SimpleDateFormat(“Z”) in Java
- SimpleDateFormat(“HH.mm.ss”) in Java
- SimpleDateFormat(“hh:mm:ss a”) in Java
- SimpleDateFormat(“HH:mm:ss Z”) in Java
- SimpleDateFormat('zzzz') in Java
- Set Date patterns with SimpleDateFormat in Java
- Display hour with SimpleDateFormat(“H”) in Java
- Display minutes with SimpleDateFormat(“m”) in Java
- Display today’s date in Java with SimpleDateFormat
- Display hour with SimpleDateFormat(“hh”) in Java
- Display seconds with SimpleDateFormat(“s”) in Java
- Display time zone with SimpleDateFormat(“z”) in Java
- Formatting day of week using SimpleDateFormat in Java
- How to format year using SimpleDateFormat in Java?

Advertisements