Home Tutorial Java Corejava Javatext Parsing a Date Using a Custom Format

 
 

Share on Google+Share on Google+
Parsing a Date Using a Custom Format
Posted on: February 16, 2005 at 12:00 AM
Advertisement
In this section, you will learn how to parse a date using a custom format.

Parsing a Date Using a Custom Format

In this section, you will learn how to parse a date using a custom format.

Java has provide many classes for handling date and time. Among them, SimpleDateFormat is easier to use for formatting and parsing the dates. You have to use a pattern of characters to specify the format to parse the date with this class. You can also specify the locale if you want to parse the date in different language.

In the given example, we have specified the pattern, in which we want to parse the date, inside the constructor of the class SimpleDateFormat.  Now to parse the date, we have used the parseObject() method of Format class. This method parses the text and produce an object. Then we have used toString() method to convert the date object into string.

Here is the code:

import java.text.*;
import java.util.*;

public class ParseDate {
	public static void main(String[] args) throws Exception {
		Format formatter = new SimpleDateFormat("MMMM dd,yyyy HH:mm");
		Date date = (Date) formatter.parseObject("September 29,2010 12:25");
		System.out.println(date.toString());
	}
}

Output:

Wed Sep 29 12:25:00 GMT+05:30 2010
Advertisement

Related Tags for Parsing a Date Using a Custom Format:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 16, 2005

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com