Parsing the Time Using a Custom Format


 

Parsing the Time Using a Custom Format

In this section, you will learn how to parse the time using custom format.

In this section, you will learn how to parse the time using custom format.

Parsing the Time Using a Custom Format

In this section, you will learn how to parse the time using custom format.

In the previous section, we have discussed the formatting of time. Parsing is just reverse of it. It converts a string into an internal representation of different objects. Here we are going to parse the time with custom format.

You can see in the given example, we have used a pattern of special characters inside the constructor of class SimpleDateFormat to specify the format of the time to parse. Then we have invoked the method parse(). This method parses the text to produce an object. Now, in order to convert this date object into string, we have used toString() method and display it on console.

Here is the code:

import java.util.*;
import java.text.*;

public class ParsingTime {
	public static void main(String[] args) throws Exception {
		DateFormat df = new SimpleDateFormat("hh.mm.ss a");
		Date date = (Date) df.parse("05.50.33 PM");
		System.out.println(date.toString());
	}
}

Output:

Thu Jan 01 17:50:33 GMT+05:30 1970

Ads