Home » Core Java » text » Parse custom formatted date with SimpleDateFormat

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Parse custom formatted date with SimpleDateFormat

With this example we are going to demonstrate how to parse custom formatted date with SimpleDateFormat. SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows formatting (date -> text), parsing (text -> date), and normalization. In short, to parse custom formatted date with SimpleDateFormat you should:

  • Create a new SimpleDateFormat, using a String pattern. The pattern describes the date and time format.
  • Invoke the parse(String source) API method to parse text from the beginning of the given string to produce a date. It will return a Date parsed from the string.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ParseCustomFormattedDateWithSimpleDateFormat {
	
	public static void main(String[] args) {
		
		
		try {
			
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
			
			// parse string in custom format into date object
			Date date = df.parse("10/11/2011 16:24");
			System.out.println(date);
			
		}
		catch (ParseException pe) {
			System.out.println("Parse Exception : " + pe.getMessage());
		}
		
	}

}

Output:

Thu Nov 10 16:24:00 EET 2011

 
This was an example of how to parse custom formatted date with SimpleDateFormat in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments