Tags

,

While ago, I wrote about how to convert a date into user defined patterns here where date object was passed.

Same can be done when the input date is a String.


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

public class StringtoDate {

public static void main(String[] args) {

// Input Date as String

String strDate = "1980/04/28";

try {
// Creating SimpleDateFormat object with source string date format
SimpleDateFormat sourceformatter = new SimpleDateFormat("yyyy/MM/dd");

// parsing the string into date object
Date date = sourceformatter.parse(strDate);
System.out.println("date is : " + date);

//Creating SimpleDate object with required date format
SimpleDateFormat resultformatter = new SimpleDateFormat("dd/MM/yyyy");

String resultDate = resultformatter.format(date);
System.out.println("converted date is : " +resultDate);

} catch (ParseException pe){
System.out.println("Parse Exception: " + pe);
}

}

}

Advertisements