Java Date Formatter

rajputhch picture rajputhch · Apr 7, 2011 · Viewed 86.3k times · Source

I am getting date format as "YYYY-mm-dd hh:mm" as formatter object.

How can I format the input formatter object to get only "YYYY-mm-dd";?

Answer

Harry Joy picture Harry Joy · Apr 7, 2011

I am getting date format as "YYYY-mm-dd hh:mm" as formatter object. How can i format the input formatter object to get only "YYYY-mm-dd";

You can not have date as YYYY-mm-dd it should be yyyy-MM-dd. To get date in yyyy-MM-dd following is the code:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);