In this section, you will learn how to parse a date using a custom format.
In this section, you will learn how to parse 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 |