java.text.SimpleDateFormat parse(String text,ParsePosition pos)

Description :

This java tutorial shows how to use the parse(String text,ParsePosition pos)  method of SimpleDateFormat class of java.text package.

This method can best described as:

  • The parse(String text,ParsePosition pos) parses text from a string to produce a Date.
  • The method attempts to parse text starting at the index given by pos.
  • If parsing succeeds, then the index of pos is updated to the index after the last character used.
  • This method was specified by parse in class DateFormat

Method Syntax :

public Date parse(String text,ParsePosition pos)

Parameter Input :

 

DataType Parameter Description
String text A String, part of which should be parsed.
ParsePosition pos A ParsePosition object with index and error index information as described above.

 

Method Returns :

This method returns a Date parsed from the string. In case of error, returns null.

Compatibility Version :

Requires Java 1.2 and up

Exception :

The following exception will be thrown if the text or pos method argument is null

NullPointerException

Discussion :

This method along with other parse method of SimpleDateFormat class will be your bestfriend in dealing with converting of date String input into fully qualified Date format. This is essential to master as the dates you will be dealing with are mostly in String. And most of the requirements in programming regarding dates is to transform one format to another and you cannot do that if dates are in String. You have the parse it and use other useful methods of SimpleDateFormat to transform the information into your requirements.

Java Code Example :

This java example source code demonstrates the use of parse(String text,ParsePosition pos) method of SimpledDateFormat class. We will be asking for a user input and transform this into other date format. This would be a good exercise in dealing with date transformation.

This example is a little bit tricky because we have padded the user input with extra characters “date:”. This is to demonstrate the use of ParsePosition method argument.

package com.javatutorialhq.java.examples;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * sparse(String text,ParsePosition pos) method 
 * of SimpleDateFormat class
 */

public class ParseTextParsePositionExample {

	public static void main(String[] args) throws InterruptedException {
		
		// ask for user input
		System.out.print("Enter date in yyyy-mm-dd:");
		Scanner s = new Scanner(System.in);
		
		// padded the user input with extra string date:
		String input = "date:"+s.nextLine();
		
		// instantiate the formatter to desired date format
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		// parse the user input considering the padded string "date""
		Date date = sdf.parse(input,new ParsePosition(5));
		
		// printing the date
		System.out.println(date);
		s.close();
		
	}

}

Sample Output :

Running the parse(String text,ParsePosition pos) method example source code of Calendar class will give you the following output:

SimpleDateFormat parse(String text,ParsePosition pos) method example

SimpleDateFormat parse(String text,ParsePosition pos) method example

Exception Scenario :

If we modify our code and have the following

Date date = sdf.parse(null,new ParsePosition(5));

the following exception will be thrown.

Enter date in yyyy-mm-dd:12-03-2014
Exception in thread "main" java.lang.NullPointerException
	at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1439)
	at com.javatutorialhq.java.examples.ParseTextParsePositionExample.main(ParseTextParsePositionExample.java:29)

Similar Method :

  • N/A

Suggested Reading List :

References :