[SalesForce] How to convert the following date format in salesforce

I have my date in following format:
2014-01-20 00:00:00
I want it in the following format
1/20/2014(mm/dd/yyyy)
But i want it as date variable not as a string.

Best Answer

You already have a date 'as a date', not as a string. The format you are talking about (2014-01-20 00:00:00) is just a visual representation of a value contained in variable of a DateTime type. If you want the date to appear in the specified format (1/20/2014), just output it using format() method:

Datetime yourDate = Datetime.now();
String dateOutput = yourDate.format('dd/MM/yyyy');