Convert string to date then format the date

Mike :

I am formatting a string to a date using the code

String start_dt = '2011-01-01';

DateFormat formatter = new SimpleDateFormat("YYYY-MM-DD"); 
Date date = (Date)formatter.parse(start_dt);

But how do I convert the date from YYYY-MM-DD format to MM-DD-YYYY format?

MByD :

Use SimpleDateFormat#format(Date):

String start_dt = "2011-01-01";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD"); 
Date date = (Date)formatter.parse(start_dt);
SimpleDateFormat newFormat = new SimpleDateFormat("MM-dd-yyyy");
String finalString = newFormat.format(date);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related