-2

I have a date :

String endDate = "2015-07-22 00:00:00";

I construct a formatter for this date :

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

But how to convert date to format yyyy-MM-dd? I want to get only 2015-07-22.

Tim
  • 39,651
  • 17
  • 123
  • 137
Sergey Shustikov
  • 14,165
  • 9
  • 62
  • 113

2 Answers2

0

This is way simpler than you'd think.

You can just use

new SimpleDateFormat("yyyy-MM-dd");

It ignores everything that doesn't fit the format.

Tim
  • 39,651
  • 17
  • 123
  • 137
0
 public static void main(String[] args) throws ParseException {
        String date = "2015-07-22 00:00:00";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = dateFormat.parse(date);
        String newDateString = dateFormat.format(date1);
        System.out.println(newDateString);
    }

output

2015-07-22
Ankur Singhal
  • 25,030
  • 13
  • 76
  • 111