Java SimpleDateFormat Example

In Java SimpleDateFormat article we’ll see how to use java.text.SimpleDateFormat class, which provides methods to format date to string or parse string to date. The SimpleDateFormat is a class which inherits java.text.DateFormat class.

Creating a SimpleDateFormat Instance:

The pattern String parameter used for parsing and formatting of dates. The Pattern Examples are given at the end of the article.

Related Links:

Java Interview Questions

Formatting Dates:

Output:

Parsing Dates:

Java SimpleDateFormat Example: Date to String

Let’s see an example to format date and time in java using java.text.SimpleDateFormat class.

Output:

Java SimpleDateFormat Example: String to Date

Let’s see the simple example to parse string into date using java.text.SimpleDateFormat class.

Output:

Pattern Examples:

Let’s look into few pattern examples:

PatternExample
dd-MM-yy25-09-17
dd-MM-yyyy25-09-2017
MM-dd-yyyy09-25-2017
yyyy-MM-dd2017-09-25
yyyy-MM-dd HH:mm:ss2017-09-25 18:07:24
yyyy-MM-dd HH:mm:ss.SSS2017-09-25 18:08:33.627
yyyy-MM-dd HH:mm:ss.SSSZ2017-09-25 18:09:11.827+0000
dd MMMM yyyy zzzz25 September 2017 Coordinated Universal Time

References:

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

 

Latest posts by Sushma Baliza (see all)

You may also like...