How to Convert String to Date Object Using Java SimpleDateFormat


In this example we will show how to parse a string containing date and time into Date object with Java SimpleDateFormat class.

Source Code

package com.beginner.examples;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ParsingCustomFormat {

	public static void main(String[] args) {
		
		//create oSimpleDateFormat.
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
		Date date;
		try {
			/*
			 * Parse string containing specified format into date object.
			 */
			date = sdf.parse("13/06/2019");
			System.out.println(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}

}

Output:

Sun Jun 23 00:00:00 CST 2019

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments