SimpleDateFormat Example with Formats, Patterns and Date Conversion

Filed Under: Java

SimpleDateFormat is the Java class for formatting and parsing dates with Locale support. You can format the date to text and vice-versa using SimpleDateFormat class.

SimpleDateFormat Class

This class is very useful in formatting date to display in UI in specific pattern. SimpleDateFormat is the concrete class that extends DateFormat abstract class.

Date and Time formats are specified by patterns, which are nothing but a String. In pattern, every letter has its specific meaning. For example, y is for the year, M is for a month in year and m is for minutes in Hour.

SimpleDateFormat Examples

In the below code, we will see the different usage of SimpleDateFormat class with different string patterns.


package com.journaldev.java;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 *
 * @author Pankaj
 * This class demonstrates different scenarios and usage of SimpleDateFormat class
 *
 */
public class SimpleDateFormatExamples {

	public static void main(String[] args) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat();
		Date date = new Date();

		//Simple Date Formatting Examples
		sdf.applyPattern("MM/dd/yyyy");
		System.out.println("MM/dd/yyyy Pattern: "+sdf.format(date));

		//Date format with extra strings
		sdf.applyPattern("hh:mm 'o'' Clock on' MMMM dd yyyy");
		System.out.println("hh:mm 'o'' Clock on' MMMM dd yyyy Pattern: "+sdf.format(date));

		//Date Formatting with Locale settings
		sdf = new SimpleDateFormat("dd MMMM yyyy zzzz G", Locale.GERMAN);
		System.out.println("dd MMMM yyyy zzzz G Pattern: "+sdf.format(date));

		//Parsing String to Date Object
		System.out.println("Native Date Format:"+date.toString());
		String dateToParse = date.toString();
		sdf = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);
		System.out.println("Parsed Date as String:"+sdf.parse(dateToParse).toString());

		//using Timezone in parsing
		sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
		Date parsedDate = sdf.parse("23-03-1984");
		System.out.println("Date with Default TimeZone: "+parsedDate);
		sdf.setTimeZone(TimeZone.getTimeZone("IST"));
		parsedDate = sdf.parse("23-03-1984");
		System.out.println("Date with IST TimeZone: "+parsedDate);

		//Strict Parsing
		/**
		 * By default, SimpleDateFormat parsing is very lenient. It tries to give correct answer rather
		 * than throwing Exception. What it means is that if the String to parse is "32-May-2011" it will
		 * treat it as 31-May-2011 + one day and gives output of 01 Jun 2011
		 */
		sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);
		System.out.println("Lenient Parsing example: "+sdf.parse("32-05-2011"));
		//Making the Parsing Strict
		sdf.setLenient(false);
		// Below line should throw ParseException
		System.out.println("Lenient Parsing example: "+sdf.parse("32-05-2011"));

	}

}

Output of the above program is:


Date with Default TimeZone: Fri Mar 23 00:00:00 GMT+00:05 1984
Date with IST TimeZone: Thu Mar 22 18:35:00 GMT+00:05 1984
Lenient Parsing example: Wed Jun 01 00:00:00 GMT+00:05 2011
Exception in thread "main" java.text.ParseException: Unparseable date: "32-05-2011"
	at java.text.DateFormat.parse(DateFormat.java:337)
	at com.journaldev.java.SimpleDateFormatExamples.main(SimpleDateFormatExamples.java:60)

I hope this program will help you in working with SimpleDateFormat in Java.

Update: Java 8 has come up with new Date Time API that is way better than older date/time classes. If you are working on Java 8, then you should consider using them. For more details, please read Java Date Time API Tutorial.

Comments

  1. article says:

    so michelle phan posted on Facebook Xiaxue’s blog.. which i have never seen during my life. it seemed interesting and so i clicked and read. i was laughing pretty…

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages