Java SimpleDateFormat parse() Example
SimpleDateFormat class parse() method example. This example shows you how to use parse() method.
Syntax is : public Date parse(String text, ParsePosition pos)
This method parses text from a string to produce a Date. The method attempts to parse text starting at the index given by pos.
Here is the code.
/**
* @(#) ParseSimpleDateFormat.java
* A class representing use of method parse() of SimpleDateFormat
class in java.text Package.
* @Version 14-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class ParseSimpleDateFormat {
public static void main(String[] args) {
// Create new Date object that will be initialized to the current date.
Date date = new Date();
System.out.println("Date by Date class \t\t: " + date.toString());
SimpleDateFormat sdf;
// Create new SimpleDateFormat object that returns default pattern.
sdf = new SimpleDateFormat("dd-MM-yyyy");
String dateFormat = sdf.format(date);
System.out.println("Date by SimpleDateFormat class : " + dateFormat);
// parse method call for SimpleDateFormat object.
ParsePosition pos = new ParsePosition(0);
Date parseDate = sdf.parse("24-12-2008", pos);
System.out.println("My B'Dy will be on \t\t: " + parseDate);
}
} |
Output of the program.
Date by Date class : Sat Jun 14 13:10:57 GMT+05:30 2008
Date by SimpleDateFormat class : 14-06-2008
My B'Dy will be on : Wed Dec 24 00:00:00 GMT+05:30 2008 |
|