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

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: The method takes two 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 Value: The method either returns the Date parsed from the string or Null in case of an error.

Below programs illustrate the working of parse() Method of DateFormat:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate parse() method
  
import java.text.*;
import java.util.Calendar;
  
public class DateFormat_Demo {
    public static void main(String[] args)
    {
        DateFormat DFormat
            = new SimpleDateFormat("MM/ dd/ yy");
  
        try {
            Calendar cal = Calendar.getInstance();
  
            // Use of parse() method to parse
            // Date From String
            String dt = "10/ 27/ 16";
            System.out.println("The unparsed"
                               + " string is: " + dt);
  
            cal.setTime(DFormat.parse(dt));
            System.out.println("Time parsed: "
                               + cal.getTime());
        }
        catch (ParseException excpt) {
            excpt.printStackTrace();
        }
    }
}

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to illustrate parse() method
  
import java.text.*;
import java.util.Calendar;
  
public class DateFormat_Demo {
    public static void main(String[] args)
    {
        DateFormat DFormat
            = new SimpleDateFormat("MM/ dd/ yy");
  
        try {
            Calendar cal = Calendar.getInstance();
  
            // Use of parse() method to parse
            // Date From String
            String dt = "01/ 29/ 19";
            System.out.println("The unparsed"
                               + " string is: " + dt);
  
            cal.setTime(DFormat.parse(dt));
            System.out.println("Time parsed: "
                               + cal.getTime());
        }
        catch (ParseException excpt) {
            excpt.printStackTrace();
        }
    }
}

chevron_right


Output:

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

Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse(java.lang.String, %20java.text.ParsePosition)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.