Tutorialspoint examples

Learn java tutorial with examples for beginners online

Convert string to date java program


Java convert string to date java example

package com.tutorialspointexamples;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class StringToDate {
  public static void main(String args[]){
	  try {
		String dateString = "18-Mar-2018";
		  DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
		  Date date = df.parse(dateString);
		  System.out.println("Date: " + date);
	} catch (ParseException e) {
		e.printStackTrace();
	}
  }
}

Output:

Date: Sun Mar 18 00:00:00 IST 2018

Download this example.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status