How to convert String to Date in java?

DeeKay October 5, 2011 at 11:15 pm · java.util

Below code shows how to convert String to Date in java. It converts String Object to java.util.Date

/****************************************************************************************
 * Created on 10-2011Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.javautil;

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

/**
 * Created by http://kodehelp.com
 * Date: 10/5/11
 */
public class StringToDate {

        public static void main(String[] args) {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

            try {
                Date yesterday = df.parse("04/12/1981");
                System.out.println("Yesterday = " + df.format(yesterday));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
}