Open In App

DateFormat parse(string , ParsePosition) Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

DateFormat class of java.text package is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time.

Note: DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner.  

Package-view:

java.text Package
    DateFormat Class
        parse(string , ParsePosition) Method

The parse(String the_text, ParsePosition position) method of DateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.

Syntax: 

public abstract Date parse(String the_text, ParsePosition position)

Parameters: It takes 2 parameters: 

  • the_text: This is of the String type and refers to the string which is to be parsed to produce the date.
  • position: This is of ParsePosition object type and refers to the information of the starting index of the parse.

Return Type: Returns the Date parsed from the string or Null in case of an error.

Example 1:

Java




// Java Program to Illustrate parse() Method
// of DateFormat Class
 
// Importing required classes
import java.text.*;
import java.util.Calendar;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of DateFormat class inside main()
        DateFormat DFormat
            = new SimpleDateFormat("MM/ dd/ yy");
 
        // Try block to check for exceptions
        try {
 
            Calendar cal = Calendar.getInstance();
 
            // Parsing date From string
            // using parse() method of DateFormat class
 
            // Custom string date
            String dt = "10/ 27/ 16";
 
            // Printing the above unparsed date
            System.out.println("The unparsed"
                               + " string is: " + dt);
 
            // Parsing date using parse() method
            cal.setTime(DFormat.parse(dt));
 
            // Printing the parsed time
            System.out.println("Time parsed: "
                               + cal.getTime());
        }
 
        // Catch block to handle exceptions
        catch (ParseException except) {
 
            // Display exceptions with line number
            // using printStackTrace() method
            except.printStackTrace();
        }
    }
}


Output: 

The unparsed string is: 10/ 27/ 16
Time parsed: Thu Oct 27 00:00:00 UTC 2016

 

Example 2:

Java




// Java Program to Illustrate parse() Method
// of DateFormat Class
 
// Importing required classes
import java.text.*;
import java.util.Calendar;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of DateFormat class
        DateFormat DFormat
            = new SimpleDateFormat("MM/ dd/ yy");
 
        // Try bloc kto check for exceptions
        try {
 
            // Getting instance from calendar
            Calendar cal = Calendar.getInstance();
 
            // Parsing date from string
            // using parse() method
            String dt = "01/ 29/ 19";
 
            // Displaying the unparsed date
            System.out.println("The unparsed"
                               + " string is: " + dt);
 
            // Parsing date
            cal.setTime(DFormat.parse(dt));
            System.out.println("Time parsed: "
                               + cal.getTime());
        }
 
        // Catch block to handle the exceptions
        catch (ParseException except) {
 
            // Display exception with line number
            // using printStackTrace() method
            except.printStackTrace();
        }
    }
}


Output

The unparsed string is: 01/ 29/ 19
Time parsed: Tue Jan 29 00:00:00 UTC 2019


Last Updated : 24 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads