Java DateFormat parse() Example
DateFormat class parse() method example. This example shows you how to use parse() method.
Syntax is : public Date parse(String source) throws ParseException
This method parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given
string.
Here is the code.
/**
* @(#) ParseDateFormat.java
* A class representing use of method parse() of DateFormat
class in java.text Package.
* @Version 13-May-2008
* @author Rose India Team
*/
import java.util.*;
import java.text.*;
class ParseDateFormat {
public static void main(String[] args) throws ParseException {
// Create new Date object that will be initialized to the current date.
Date date = new Date();
System.out.println("Date by Date class : " + date.toString());
// Create object for default MEDIUM/SHORT DateFormat
DateFormat dateFormat = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.SHORT);
// parse method call.
Date parseDate = dateFormat.parse("Jan 1, 2004 9:00 AM");
System.out.println("After parse method : " + parseDate.toString());
}
} |
Output of the program.
Date by Date class : Fri Jun 13 18:26:52 GMT+05:30 2008
After parse method : Thu Jan 01 09:00:00 GMT+05:30 2004 |
|